Home > Software engineering >  how can i import main functin of another dart file
how can i import main functin of another dart file

Time:05-17

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!

CodePudding user response:

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");
}
  • Related