How can I run unittests in Dart Editor? How can I run unittests in Dart Editor? dart dart

How can I run unittests in Dart Editor?


I had exactly the same problem. I wanted to unit test the behaviour of my widgets (from the dart editor v1.3.3) having mocked out the UI elements, but found that, because I was importing dart:html, I was forced to run the tests as if they were a web app with one of the 'Run in browser' menu options.

It seems that as soon as you import dart:html or a package that does so you're stuck with the 'Run in browser' options for your unit tests (creating another package to test the original one doesn't solve the problem either, for the same reason, it has to import the dart:html one). And yes, attempting to run regular unit tests with the 'Run in browser' options just fails silently and does absolutely nothing.

The simple solution is to use an html file to run your tests. You can adapt your unit tests to output to the webpage if you like but it's not essential.

test.html

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Unit Test Results</title>  </head>  <body>    <script type="application/dart" src="test.dart"></script>    <script src="packages/browser/dart.js"></script>  </body></html>

test.dart

// Used to illustrate the problem. Importing dart:html causes// the context menu to show only the 'Run in browser' options.import 'dart:html';import 'package:unittest/unittest.dart';// Required if you want to use useHtmlConfiguration().import 'package:unittest/html_config.dart';void main() {    // Required if you want to output the unit test results    // to the HTML page rather than the console.    useHtmlConfiguration();    test ('simple test', () => expect(true, isTrue, reason:"true isn't true"));}


I actually had the exact same problem as described in the question. After some searching I discovered my directory structure was incorrect.

my directory structure was as follows:

/projectname  /web    index.html    main.dart    /test      test_file.dart

This is wrong. I changed my directory structure to the following:

/projectname  /web    index.html    main.dart  /test    test_file.dart

As you can see, i moved the /test directory out of the /web directory. As soon as I did this, I was now able to right click the test_file.dart file and select 'run' in the context menu (not 'run as javascript' or 'run in dartium') and see the tests being run in the built-in console in the dart editor.

For more information: the full pub package layout conventions can be found here: https://www.dartlang.org/tools/pub/package-layout.html

information about unit tests in dart can be found here: https://www.dartlang.org/articles/dart-unit-tests/#configuring-the-test-environment (including configuring the test environment for stuff like html output etc...)


You say you have a web application project so you want test code that runs in the browser?I have never experienced that I have run as javascript or run in Dartium for console applications. Did you import 'dart:html' in this dart file?

Anyways maybe this answers your question:

You can run unittests testing web code with content_shell.
The Dart installation directory contains a script to download content_shell (it is not part of the Dart installation package to keep it small).

You actually run an HTML page:

content_shell --dump-render-tree polymer_ajax.html

I haven't tried but when you build your test code to JavaScript using

pub build test

you should be able to run the result using content_shell.
DartEditor doesn't show a run option for a bash script (just tried) so you would need to build a Dart script that invodes content_shell.

The Dart team has mentioned several times recently that they want to improve the test story but the infrastructure for run/build is still work in progress. When this has settled I guess the test support will be worked on next.