Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run unittests in Dart Editor?

I have the web application project in Dart Editor and I want to write some unittests for it. I tried to create .dart file with a simple unit test in the same project (with main function etc) but in context menu I had only 'run as javascript' and 'run in dartium' options (when I click any of them nothing happens). How can I run unittest as console app from Dart Editor? What is the most convenient way of doing such tests?

like image 394
kolar Avatar asked Apr 19 '14 16:04

kolar


People also ask

How do I run a unit test file?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

What is a test at dart?

Unit Testing involves testing every individual unit of an application. It helps the developer to test small functionalities without running the entire complex application. The Dart external library named "test" provides a standard way of writing and running unit tests.


2 Answers

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"));
}
like image 178
T C Avatar answered Sep 27 '22 18:09

T C


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...)

like image 26
Yanik Ceulemans Avatar answered Sep 27 '22 20:09

Yanik Ceulemans