How can you unit test lib / src files in Dart?

The My Dart package is currently laid out as follows:

lib/
  mypackage.dart
  src/
    mypackage_util.dart
test/
  mypackage_test.dart

      

Inside mypackage.dart

, I use import strategy part

, part of

for use mypackage_util.dart

in mypackage.dart

, in accordance with the recommendation of the convention Pub layout .

On the test side, I took inspiration from the Seth Ladd UnitTest Example , which shows him building a new library for his tests, which makes sense to me.

Unfortunately, this results in the inability to import mypackage_util.dart

into mypackage_test.dart

, which means that I cannot test classes or functions from mypackage_util.dart

or anything in src/

.

The solutions I present are as follows:

  • Make it mypackage_test.dart

    part of the core library, but that seems to make it impossible to just run the test code offline.

  • Take it mypackage_util.dart

    from src/

    , but that seems to mean you can never unit test src/

    code in packages, which seems silly and exposes code I don't want.

Should I take one of the above approaches, or am I missing something?


Update:

The above situation was caused by a library import conflict (see Chapter 2. Dart Tour Libraries and Scope for Resolving Library Conflicts). See comments below .

+3


source to share


1 answer


If lib/mypackage.dart

declares a library and includes lib/src/mypackage_util.dart

with part/part of

, you just need to include lib/mypackage.dart

in your test - or is your problem that you want to test the private classes / functions contained in lib/src/mypackage_util.dart

?



+2


source







All Articles