Testing utilities for users of package:build
.
Installation
This package is intended to only be as a development dependency for users
of package:build
, and should not be used in any production code. Simply
add to your pubspec.yaml
:
dev_dependencies:
build_test:
Running tests
To run tests, you should go through the dart run build_runner test
command.
This will compile all your tests to a temp directory and run them using
dart run test
. If you would like to see the output directory, you can use the
--output=<dir>
option to force the output to go to a specific place.
Forwarding additional args to dart run test
It is very common to need to pass some arguments through to the eventual call
to dart run test
. To do this, add all those args after an empty --
arg.
For example, to run all chrome platform tests you would do
dart run build_runner test -- -p chrome
.
Debugging web tests
This package will automatically create *.debug.html
files next to all your
*_test.dart
files, which can be loaded in a browser from the normal
development server (dart run build_runner serve
).
Note: In order to run the tests this way, you will need to configure them
to be compiled (by default we only compile *.browser_test.dart
files). You
can do this in your build.yaml file, with something like the following:
targets:
$default:
builders:
build_web_compilers:entrypoint:
generate_for:
- test/**_test.dart
- web/**.dart
You may also view an index of links to every *.debug.html
file by navigating
to http://localhost:8081
(or wherever your test
folder is being served).
Writing tests for your custom Builder
In addition to assiting in running normal tests, this package provides some
utilities for testing your custom Builder
classes.
See the test
folder in the build
package for more examples.
Run a Builder
within a test environment
Using testBuilder
, you can run a functional test of a
Builder
, including feeding specific assets, and more. It automatically
creates an in-memory representation of various utility classes.
Exposing actual package sources to testBuilder
You can expose real package sources to the builder in addition to your in
memory sources, by passing a PackageAssetReader
to the reader
parameter:
testBuilder(yourBuilder, {}/* test assets here */,
reader: await PackageAssetReader.currentIsolate());
You can pass any custom AssetReader here, which will be used as a fallback for any source not defined in the source assets map.
Resolve source code for testing
Using resolveAsset
and
resolveSource
, you can resolve Dart source code into a
static element model, suitable for probing and using within tests of code you
might have written for a Builder
:
test('should resolve a simple dart file', () async {
var resolver = await resolveSource(r'''
library example;
class Foo {}
''');
var libExample = resolver.getLibraryByName('example');
expect(libExample.getType('Foo'), isNotNull);
});