0

I have written a simple command-line Dart app. The functionality of the app is in file app.dart located in the bin directory.

bin/app.dart

import 'dart:io';
void main(List<String> args) {
   //(...)
}

I'd like to test the main method with unit tests. For this purpose I have a file app_test.dart in the test directory

test/app_test.dart

import 'package:test/test.dart';
import 'package:app/app.dart';

void main() {
  test('main method runs through with exit code 0', () {
    //I'd like to run the main method from bin/app.dart here
  });
}

How can I refer to the main method in bin/app.dart from the file test/app.dart to run the main method for testing?

simon
  • 12,666
  • 26
  • 78
  • 113

1 Answers1

0

The solution is apparently to use a relative path import instead of a package import. So I changed the line

import 'package:app/app.dart';

in file test/app_test.dart

to

import '../bin/app.dart' as app;

This enabled being able to call the main method with

app.main();

from the test file.

I couldn't not easily find a reference to this behaviour in the official documents, but this Stackoverflow answer came to rescue: How to reference another file in Dart?

simon
  • 12,666
  • 26
  • 78
  • 113