简体   繁体   English

如何测试 main.dart

[英]How to test main.dart

main.dart main.dart

int sumInt(int a, int b) {
  return a + b;
}
int main(List<String> args) {
  var result;
  var a = 10;

  result = sumInt(a, 50);
  return result;
}

gao.dart高飞镖

int calculate() {
  return 6 +10;
}

gao_test.dart gao_test.dart

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

void main() {
  test("test to check add method", () {
    expect(calculate, result );
  });
}

The question is, how do I test main.dart?问题是,我如何测试 main.dart? I think importing main.dart in gao_test.dart, but there was an error importing.我想在gao_test.dart中导入main.dart,但是导入出错。 Please help me.请帮我。

You will need to import it with a prefix in order to be able to reference the main function (otherwise it is shadowed by your tests main function), so something like this (depending on where main.dart lives):您需要使用前缀导入它,以便能够引用main函数(否则它会被您的测试 main 函数遮蔽),所以是这样的(取决于 main.dart 所在的位置):

import 'package:gao/main.dart' as gao_main;
import 'package:test/test.dart';

main() {
  test('main', () {
    expect(gao_main.main([]), 60); 
  });
}

I don't think it can be done.我不认为这是可以做到的。 You can use annotation @visibleForTesting to mark anything that will be available for testing.您可以使用注释 @visibleForTesting 来标记可用于测试的任何内容。 But since test class also has a main, you can't import the "other main" to it.但是由于测试类也有一个 main,你不能将“另一个 main”导入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM