question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an option to treat '_test.dart' files outside of './test' as tests

See original GitHub issue

As a convention, we are keeping our test files next to our source files instead of in a separate test directory. Now, when we click “Run” above the test in VSCode, it fails:

Could not find an option named "name".

Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.
Exited (64)

You can reproduce this:

  1. Create a new Flutter project
  2. Add a new file lib/main_test.dart:
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('test test', () {
    expect(123, 123);
  });
}
  1. Click “Run” above test
  2. Check Debug Console

Is there any way to get this working? If not, how do you recommend structuring the test directory? We have a very big project and we want to make sure we keep our code organized.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
DanTupcommented, Feb 26, 2020

Sorry for the delay… I was hoping to come up with something better, but haven’t so far.

I’m thinking the simplest way is a setting, something like dart.allowTestsOutsideTestFolder. When set to false (the default) it will assume that _test.dart scripts outside ./test are not tests (I think this is probably most common, though I have no data). When set to true, then _test.dart anywhere will be considered a test.

CodeLens would use this too, so you’d need to enable it to see codeLens for tests outside of ./test.

If it seems like many people are hitting this and unaware of the setting, we could do something basic like a regex over the file (if it ends with _test.dart) and if it looks like it might include tests, give the user a prompt asking which way they want (and persisting that setting so we don’t ask again).

You could set the setting in User Settings (if you do this a lot, it’ll apply to all projects), but also if you have an open source/shared project doing this, you could commit it in .vscode/settings.json so that anyone opening that project will have it for that window.

How does this sound?

4reactions
maxlapidescommented, Dec 12, 2019

I’m not sure what reason there is for having a file named _test.dart that is NOT a test file. It seems to me that it is a better assumption that ALL _test.dart files are test files. It would be easier for someone who named a non-test file _test.dart to rename that file than it will be for us to completely reorganize our project to relocate all test files to a /test directory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

test | Dart Package - Pub.dev
A full featured library for writing and running Dart tests across platforms.
Read more >
Dart testing
You can use the test package to: Write single tests, or groups of tests. Use the @TestOn annotation to restrict tests to run...
Read more >
DART: Directed Automated Random Testing - People @EECS
cally the execution along alternative program paths. Together, these three techniques constitute Directed Automated Random Testing, or. DART for short.
Read more >
Automated Tests in Flutter: Native Tests or Cross-platform ...
Thorough testing needs to have both widget and E2E tests. Widget tests. ... In the _world.dart files, we can add or redefine the...
Read more >
Chapter 3. Building and testing your own Dart app
You'll build the PackList app from an entry-point HTML file that hosts the Dart script, and a Dart code file that creates and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found