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

Add option to create unit test for library #2650

Closed
bsutton opened this issue Jul 16, 2020 · 8 comments · Fixed by #2662
Closed

Add option to create unit test for library #2650

bsutton opened this issue Jul 16, 2020 · 8 comments · Fixed by #2662
Labels
in editor Relates to code editing or language features in testing Relates to test execution of Dart/Flutter tests for end users is enhancement
Milestone

Comments

@bsutton
Copy link

bsutton commented Jul 16, 2020

The eclipse ide has a nice little feature which will create a unit test for a java file.

I was hoping you could add something similar.

I have a file in:

lib/src/docker/images.dart

I open the vscode explorer to the above file and open the context menu for that file.
There is an option 'create unit test'

The option creates a library as per:
test/src/docker/images_test.dart

A nice bonus is that it generates a basic template for a unit test.

@DanTup
Copy link
Member

DanTup commented Jul 16, 2020

We currently have this command that lets you jump between test/implementation (no default keybinding though):

Screenshot 2020-07-16 at 17 49 28

It currently only shows up when the implementation/test for the open file exists. However I think in the case where you're in an implementation file and the test does not exist, we should still show the command and if you select it, prompt to create:

Screenshot 2020-07-16 at 17 50 34

If you agree, it'll create the file (and any required folders along the way) and populate it with a test stub (using flutter_test or test depending on the open project):

Screenshot 2020-07-16 at 17 51 07

What do you think?

@DanTup DanTup added this to the v3.13.0 milestone Jul 16, 2020
@DanTup DanTup added in editor Relates to code editing or language features in testing Relates to test execution of Dart/Flutter tests for end users labels Jul 16, 2020
@DanTup
Copy link
Member

DanTup commented Jul 16, 2020

Oh, I forgot to mention - the reason I'm not sure about the context menu is that I don't know if we can add things conditionally based on the paths (if only show the option if it's a test file that has an implementation, or a file inside lib).

I'll have a look though - if we can do that, we could also add this to the context menu without it showing up in places that wouldn't work.

DanTup added a commit that referenced this issue Jul 16, 2020
@bsutton
Copy link
Author

bsutton commented Jul 17, 2020

I think that works. The context menu would be nice as it improves interoperability.

@DanTup
Copy link
Member

DanTup commented Jul 29, 2020

I added a context menu to go to tests for anything in the lib folder:

https://dartcode.org/releases/v3-13/#test-file-creation

Beta is here: https://github.com/Dart-Code/Dart-Code/releases/tag/v3.13.0-beta.3

@bsutton
Copy link
Author

bsutton commented Jul 30, 2020

I've just installed the beta.

Very nice :D

@bsutton
Copy link
Author

bsutton commented Jul 30, 2020

It would be nice if the sample test script used the name of the dart file.

If the original dart file is certificate.dart

Currently you generate:

void main() {
  test('Test ...', () async {
    
  });
}

It would be nicer if it generated:

void main() {
  test('Certificate ...', () async {
    
  });
}

Also not certain if 'async' should be the default.
Almost non of my unit tests use async but then my usage patterns may not be normal as I build a lot of cli scripts that don't use any futures.

@bsutton
Copy link
Author

bsutton commented Jul 30, 2020

It should also import the original file:

import 'package:test/test.dart';
import 'package:mypackage/src/certificate.dart';

@DanTup
Copy link
Member

DanTup commented Jul 30, 2020

It would be nice if the sample test script used the name of the dart file.

Seems reasonable, I'll take a look.

Also not certain if 'async' should be the default.

I figured there was no disadvantage to having async there if you don't use it, but if you did you'd otherwise need to add it (and for Flutter, I think it's likely you always would - since many of the tester methods are async).

It should also import the original file:

Ah, yes. Though this gets a bit more tricky as we don't have easy access to the package name without parsing the pubspec. For now I might rely on people just using the auto-import completion, and maybe once #1996 is done and we have easier access to a package map, use that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in editor Relates to code editing or language features in testing Relates to test execution of Dart/Flutter tests for end users is enhancement
Projects
None yet
2 participants