Dart-code: Re-Running the last test in VS Code

Created on 3 Mar 2020  路  9Comments  路  Source: Dart-Code/Dart-Code

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?

in commands in testing is enhancement

Most helpful comment

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!

All 9 comments

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

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.

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!

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.

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.

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

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?

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajeshjeshar picture rajeshjeshar  路  4Comments

mccadi picture mccadi  路  3Comments

awfin picture awfin  路  3Comments

jascodes picture jascodes  路  4Comments

FeimiSzy picture FeimiSzy  路  4Comments