0
votes

I'm confused as to how to enable code coverage for unit tests while writing a Dart application. I have a number of non-web based tests -- they test methods of my domain model used by my web app. These methods don't need to run in the browser for unit testing.

Right now, I run them in the Idea Intellij Community Edition IDE by creating a run configuration for "Run all tests in /test directory".

I can also run them via the terminal like this: "pub run test".

I've read the following guides:

https://dart-lang.github.io/observatory/code-coverage.html

However I cannot seem to get anything to work. Following the guides above, I assume I have to do this from the terminal:

$ collect_coverage --uri=http://... -o coverage.json --resume-isolates

But I see nowhere I can get the URI mentioned. The documentation says "where --uri specifies the Observatory URI emitted by the VM."

Okaaaay... Where do I see the URI emitted by the VM?

Reading other sections of the guide, I see that I can start standalone Dart apps in the Observatory like so:

$ dart --observe <script>.dart

I have tried selecting one of the unit test files and invoking it using that command, like this:

$ dart --observe test/die_roll_test.dart
Observatory listening on http://127.0.0.1:8181/
00:00 +0: can be constructed from a String
00:00 +1: should create normalized DieRoll
00:00 +2: can denormalize
00:00 +3: can add
00:00 +4: can subtract
00:00 +5: can multiply
00:00 +6: can divide
00:00 +7: All tests passed!
vm-service: isolate(297422250)  'die_roll_test.dart:main()' has no debugger attached and is paused at exit.  Connect to Observatory at http://127.0.0.1:8181/ to debug.

Visiting URL http://127.0.0.1:8181 in my browser yields an empty page with the title 'Dart VM Observatory'.

In any case, I can try issuing the code coverage command using that URL:

$ collect_coverage --uri=http://127.0.0.1:8181 coverage.json --resume-isolates

This spits out a huge glob of text, but I don't see a coverage.json file created anywhere.

Ideally, this could be integrated with the IDE so I can choose coverage as an option when running the unit tests. But even if I have to live with command line utilities, how do I get it work?

If it helps, I'm running on Mac OS X 10.13 High Sierra, with Dart 2.0.0 and test 1.3.0.

2

2 Answers

0
votes

Follow this issue: https://github.com/dart-lang/test/issues/36

We hope to make this easier. It is possible today, but it's not nicely aligned with our test package.

0
votes

You could also try this package:

https://pub.dartlang.org/packages/test_coverage

I have no experience, but it might be worth a shot!