Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Running the last test in VS Code #2285

Closed
ened opened this issue Mar 3, 2020 · 9 comments · Fixed by #2369
Closed

Re-Running the last test in VS Code #2285

ened opened this issue Mar 3, 2020 · 9 comments · Fixed by #2369
Labels
in commands Relates to commands (usually invoked from the command Palette) in testing Relates to test execution of Dart/Flutter tests for end users is enhancement
Milestone

Comments

@ened
Copy link
Contributor

ened commented Mar 3, 2020

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?

@DanTup
Copy link
Member

DanTup commented Mar 4, 2020

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?

@DanTup DanTup added the awaiting info Requires more information from the customer to progress label Mar 4, 2020
@ened
Copy link
Contributor Author

ened commented Mar 4, 2020

@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".

@ened
Copy link
Contributor Author

ened commented Mar 4, 2020

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.

@DanTup
Copy link
Member

DanTup commented Mar 4, 2020

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!

@ened
Copy link
Contributor Author

ened commented Mar 5, 2020

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.

@DanTup
Copy link
Member

DanTup commented Mar 5, 2020

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.

@ened
Copy link
Contributor Author

ened commented Mar 7, 2020

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

@ened ened closed this as completed Mar 7, 2020
@DanTup DanTup added is question and removed awaiting info Requires more information from the customer to progress labels Mar 9, 2020
@HerrNiklasRaab
Copy link

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?

@DanTup
Copy link
Member

DanTup commented Apr 20, 2020

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!

@DanTup DanTup reopened this Apr 20, 2020
@DanTup DanTup added in commands Relates to commands (usually invoked from the command Palette) in testing Relates to test execution of Dart/Flutter tests for end users is enhancement and removed is question labels Apr 20, 2020
@DanTup DanTup added this to the v3.10.0 milestone Apr 20, 2020
DanTup pushed a commit that referenced this issue Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in commands Relates to commands (usually invoked from the command Palette) in testing Relates to test execution of Dart/Flutter tests for end users is enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants