unit-testingdartdart-unittest

Write unittest and save in the separate folder


I am trying to write a unittest for my class. First of all, i install the package unittest and create a spec folder to save all my unittest files. Then i create a test dart file with following contents:

library i18n_test;

import 'package:unittest/unittest.dart';

void main() {


}

When i run the file, i got the following error

Unable to open file: D:\Dart\i18n\bin\spec\packages\unittest\unittest.dart'file:///D:/Dart/i18n/bin/spec/i18n_test.dart': error: line 3 pos 1: library handler failed
import 'package:unittest/unittest.dart';

As i mentioned, i saved my unittest file in the spec folder, i think that because the compiler could not find the package unittest.
My Folder structure looks like:
enter image description here

What do i wrong?


Solution

  • The error is caused by a missing packages symlink in your spec folder. see https://code.google.com/p/dart/issues/detail?id=16947

    Anyway
    unit tests should be put into the test folder (i18n/test).
    This way your test code won't be included in the build output.

    It's best to have most of your code in the lib folder to make it easy to import into your tests using package imports like:

    import 'package:i18n/my_dart_file.dart';
    

    you should still be able to import from bin or web using relative imports like

    import '../bin/my_dart_file.dart';
    

    but that form is not very refactoring-friendly.