Skip to content

Re-Running the last test in VS Code #2285

Closed

Description

@ened

In our workflow, developers work on the test that's home in test/component_test.dart. The file contains a main() method and a root group, which is further split into N subgroups and tests.

In Android Studio, I am able to hover the main() function, press Control-Shift-R (I'm on a mac) and all tests in that file run. To re-run the same set of tests, a simple Control-R is needed.

This is immensely helpful to quickly iterate (until flutter testing gains things like a watcher).

However, this workflow currently seems to be impossible in VS Code. Is that a Dart-Code task (to deeply integrate with VS Code testing framework) or is it something else?

Activity

  1. DanTup commented on Mar 4, 2020

    @DanTup
    Member

    There are two commands that may be of interest, though I don't know if they solve your whole need:

    Dart: Run Test at Cursor
    This will let you run the test for where your (editor, not mouse) cursor is.

    Dart: Rerun last debug session (Cmd/Ctrl+Shift+F5)
    This will re-run the last debug session, which if executed after the command above, will re-run that test (even if the cursor is no longer in that test). This also works after running a whole group (eg. with the CodeLens links) or file.

    Do these help? Is here anything missing?

  2. added
    awaiting infoRequires more information from the customer to progress
    on Mar 4, 2020
  3. ened commented on Mar 4, 2020

    @ened
    ContributorAuthor

    @DanTup ok, just a few comments:

    Given a file like this:

    void main() {
      group('jsonEncodeAsync', () {
        test('complex types', () async {
          expect(await jsonEncodeAsync(['A', 'B']), '["A","B"]');
          expect(await jsonEncodeAsync({'A': 'B'}), '{"A":"B"}');
        });
      });
      group('jsonEncodeAsync', () {
        test('simple types', () async {
          expect(await jsonEncodeAsync(23), '23');
          expect(await jsonEncodeAsync('123'), '"123"');
          expect(await jsonEncodeAsync(23.1), '23.1');
        });
    
        test('complex types', () async {
          expect(await jsonEncodeAsync(['A', 'B']), '["A","B"]');
          expect(await jsonEncodeAsync({'A': 'B'}), '{"A":"B"}');
        });
      });
    }
    

    Then I can not run the tests for the whole file, but must select group by group. Perhaps it's not standard Dart practice to have more than 1 root-group, not sure.

    Secondly, "Dart: Rerun last debug session" would always run in "Debug" mode, whereas "Run Test At Cursor" actual has a sibling "Debug At Cursor" to explicitly enable it.
    I would look for "Re-Run with debug" and "Re-Run without debug".

  4. ened commented on Mar 4, 2020

    @ened
    ContributorAuthor

    Also, the "Run Test At Cursor" is too sensitive to the cursor position:

    Screenshot 2020-03-04 at 10 39 37

    In the screenshot, the cursor needs to be at the g in order to enable the action, but one char left to it, or the beginning of the line, will hide the action.

  5. DanTup commented on Mar 4, 2020

    @DanTup
    Member

    I can not run the tests for the whole file, but must select group by group

    You can run all tests in a file by pressing F5 with that file open (assuming you don't have a launch configuration with a hard-coded path).

    "Dart: Rerun last debug session" would always run in "Debug" mode, whereas "Run Test At Cursor" actual has a sibling "Debug At Cursor" to explicitly enable it

    Rerun last debug session should run in the same mode that the last session ran in. All it does is stores the last debug launch config (in its entirety, which includes the noDebug flag) and launches it. If that's not what you're seeing, I consider it a bug (please open a new issue with exact steps if you're seeing differences).

    Also, the "Run Test At Cursor" is too sensitive to the cursor position

    It currently just checks whether it's inside the outline node for that group, so it's working as it was intended, but I think maybe expanding the range to the start of the line if it's only whitespace is probably reasonable (please file a specific issue for this and I'll try to improve it).

    Thanks!

  6. ened commented on Mar 5, 2020

    @ened
    ContributorAuthor

    I can not run the tests for the whole file, but must select group by group

    You can run all tests in a file by pressing F5 with that file open (assuming you don't have a launch configuration with a hard-coded path).

    That is the conflict I run into a few times.
    My tests are in 'test/', and the app is in 'lib/'. When pressing F5 in a test/.. file, then VS code tries to run the application due to the default configuration.

    run config is:

            {
                "name": "Flutter",
                "request": "launch",
                "type": "dart",
                "program": "lib/main.dart",
            },

    I would really want it to just run the test at that moment. Then the editor currently shows any file inlib/, pressing F5 to run the app makes sense.

    Will check the other two items and open issues separately.

  7. DanTup commented on Mar 5, 2020

    @DanTup
    Member

    When pressing F5 in a test/.. file, then VS code tries to run the application due to the default configuration.

    This configuration is explicitly set to run lib/main.dart. This isn't actually the default, it's just what is in one of the default snippets to allow you to customise. If that's all that's in your run config, you should be able to just delete the launch.json and it'll use the default (which is essentially the same, but with no program field).

    When there's no explicit program, we will try to infer it from the open file - if it's a test file, we'll run it as a test file. Otherwise we'll use the project type (Flutter vs Dart) to try to find a suitable entry point (lib/main.dart, or bin/main.dart).

    So, if you delete the launch.json (or if you need it for other reasons, remove the program) it should do what you want when you press F5. If not, let me know - there may be room to improve the rules.

  8. ened commented on Mar 7, 2020

    @ened
    ContributorAuthor

    Ok - that solves it. Thank you for clarifying!
    Will feedback on the rules if needed.

  9. added and removed
    awaiting infoRequires more information from the customer to progress
    on Mar 9, 2020
  10. HerrNiklasRaab commented on Apr 17, 2020

    @HerrNiklasRaab

    I some sense not having the rerun last test option destroys a bit my workflow. Example Szenario:

    • You write a test
    • You write a bit implementation code
    • You run the test
    • You write a bit implementation code
    • You rerun the last debug session
    • You write a bit implementation code
    • You rerun the last debug session

    • Now you start the app because you want to see those changes in action
    • You write a bit implementation code, because something didn't work
    • Now I would like to rerun the last test, but this is not possible, because the last debug session was starting the app.

    Do I miss something here? How would this be possible with the current version?

  11. DanTup commented on Apr 20, 2020

    @DanTup
    Member

    Nothing missed, I think that's a good point. We can probably have a Re-run Last Test command that works the same, but only tracks tests. I'll take a look. Thanks!

  12. reopened this on Apr 20, 2020
  13. added
    in commandsRelates to commands (usually invoked from the command Palette)
    in testingRelates to test execution of Dart/Flutter tests for end users
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    and removed on Apr 20, 2020
  14. added this to the v3.10.0 milestone on Apr 20, 2020
  15. added a commit that references this issue on Apr 20, 2020
    7b9ffe2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in commandsRelates to commands (usually invoked from the command Palette)in testingRelates to test execution of Dart/Flutter tests for end usersis enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions