orion3
orion3

Reputation: 9935

How do I run a specific Dart _test file with pub run test?

I have a bunch of files in my test_dir and the problem is that they must be run separately, because one of them is testing the code that can only be run in the browser and the other is testing code that can only be run in the backend.

Now, I know there's a -n argument to pub run test for specifying test name, but how do I run a specific file? It's not very clear from the documentation and I wasn't able to successfully accomplish that.

Upvotes: 4

Views: 2135

Answers (2)

Argenti Apparatus
Argenti Apparatus

Reputation: 4013

If you have separated tests into separate files, say browser_test.dart and vm_test.dart, then it's simply:

pub run test test/browser_test.dart
pub run test test/vm_test.dart

See https://pub.dartlang.org/packages/test#running-tests

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657288

You can just add the directory or concrete file name

pub run test test/some_dir/some_file_test.dart

If you want to run certain tests only on console or only in the browser you can configure this per test or per file or alternatively in a project config file.

Some test files only make sense to run on particular platforms. They may use dart:html or dart:io, they might test Windows' particular filesystem behavior, or they might use a feature that's only available in Chrome. The @TestOn annotation makes it easy to declare exactly which platforms a test file should run on. Just put it at the top of your file, before any library or import declarations:

@TestOn("vm") // or @TestOn("browser")

import "dart:io";

import "package:test/test.dart";

void main() {
  // ...
}

When you run all tests like

pub run test -pchrome -pfirefox -pie

Then this test will not be run because -pvm was not included.

This allows fine-grained configuration like

For example, if you wanted to run a test on every browser but Chrome, you would write @TestOn("browser && !chrome").

The test package has a great README for all other configuration options.

Upvotes: 5

Related Questions