Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why `#import("dart:unittest")` can't run?

I write some dart test code:

#import("dart:unittest");
main() {
  test('this is a test', () {
    int x = 2+3;
    expect(x).equals(5);
  });
}

It doesn't display any error in dart editor, but when I press the "run" button, it reports:

Do not know how to load 'dart:unittest''file:///home/freewind/dev/dart/editor
/samples/shuzu.org/test/model_test.dart': 
Error: line 1 pos 1: library handler failed
#import("dart:unittest");
^

I see there is a "dart:unittest" library in my dart-sdk. Why it can't be run?

like image 525
Freewind Avatar asked May 31 '12 04:05

Freewind


2 Answers

Unfortunately, the unittest library is not yet wired into the dart: namespace. Until that happens, if it ever happens, you'll need to use a relative path to get to the unittest library.

Something like:

#import('path-to-dart/lib/unittest/unitest.dart');

More examples are here: http://api.dartlang.org/unittest.html

like image 132
Seth Ladd Avatar answered Nov 15 '22 05:11

Seth Ladd


This page keeps showing up in Google results for dart and unittest, so I thought I would add an update. The unittest library is now installed quite easily through pub, Dart's package manager. To do this, make sure that you:

check Add pub support when you create a new Dart application. Then add (or uncomment) the dependency for the unittest package in your pubspec.yaml file. That file should look like this:

name:  range
description:  A sample application

dependencies:
    unittest: { sdk: unittest }

Run pub install (although if you are using Dart Editor, this command should automatically get run for you). Then, in the file where you will be writing your tests, add this import declaration:

import "package:unittest/unittest.dart";

And you should be good to go.

like image 23
Shailen Tuli Avatar answered Nov 15 '22 07:11

Shailen Tuli