how can i import main functin of another dart file

40 views Asked by At

How can I verify that a print is called for in dart unit tests?

I am writing a sample code for textbooks and want to try it out. There are many examples that use printing for simplicity. I want to run my unit tests to make sure the print is called with the correct input but I have a problem importing the main function in another dart file.

Thank you!

1

There are 1 answers

0
lrn On BEST ANSWER

You can import any library, including a script with a main method. The problem is that your own script's main method shadows the import.

The solution is to import the library with a prefix:

import "other_library.dart" as testee;

void main() {
  print("Testing something");
  testee.main();  // Uses a prefixed name to avoid name conflict.
  print("Testing done");
}