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?
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.
Ok, I will describe solution that works best for me.
I created separate project for tests in Dart Editor (polymer web application - the same as target project). Main .html
file in test project imports .dart
file that looks like:
library foo_test;
import '../../foo_project/web/foo.dart'; //test target
import '../packages/mock/mock.dart';
import '../packages/unittest/unittest.dart';
import '../packages/unittest/html_config.dart';
void main() {
useHtmlConfiguration();
test("sample test", () {
expect(foo_function(), equals(expected_result) );
});
}
useHtmlConfiguration()
creates some nice page with all tests listed. I also tried useHtmlEnhancedConfiguration()
as described in https://www.dartlang.org/articles/dart-unit-tests/ but it doesn't print stack traces (neither in page nor in console) so it is rather useless for me. There is also useHtmlInteractiveConfiguration()
in documentation but it doesn't exist in unittest
package.
© 2022 - 2024 — McMap. All rights reserved.
content_shell
. Anyway, nice to hear that testing is important for dart team. – Wrathful