所以我正在测试一个有多个页面的应用程序。
有时,我只是想从选择的页面开始测试。
但是,我不知道该怎么做。
我看到的每个地方,每个人总是在开始他们的集成测试时调用main()
。
这有道理,但我的情况是独特的。我试图导入我想要开始的页面,我尝试调用主类,但我得到以下错误:
The constructor returns type 'dynamic' that isn't of expected type 'widget'.
我的堆栈也声明:
When the exception was thrown, this was the stack:
#4 main.<anonymous closure>.<anonymous closure> (file:///D:/WEBDEV/EndevStudios/MedicalApp/gshDevWork/medical-app-frontend/integration_test/basic_app_test.dart:94:7)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
我想我要调用的类不是那样构建的。下面是该代码的开头部分:
class WelcomePage extends StatelessWidget {
final String title = "Welcome!";
final String titleDev = "Take Control of your Health!";
final String boxDescription =
"It is for information purposes only and help you access health professionals through virtual means as well as screen for a selection of diseases.";
final String innerContainerTextTitle =
"WARNING! This application is not a diagnosing tool.";
final String descriptionTitle = 'App Features:';
final String descriptionTitle1 =
"Technology to analyze a selection of diseases.";
final String descriptionTitle2 =
"Resources to learn about different types of diseases; typical treatments; and management standards.";
final String _titleImagePath = 'assets/appbar/diagnosis.webp';
WelcomePage();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: _buildAppbar(context),
body: Container(
width: displayWidth(context),
height: displayHeight(context),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// _logoImage(context),
_welcomeInfo(context),
],
)),
);
}
在我的测试中我调用app.WelcomePage();
您可以使用pumpWidget从任何页面开始假设这里WelcomePage不是你的第一个屏幕,你想从WelcomePage开始测试,那么你可以,但记住一件事,必须用MaterialApp包装你的WelcomePage小部件
testWidgets('Welcome Page Test', (WidgetTester tester) async {
/// build your app tree first
await tester.pumpWidget(const MaterialApp(home:WelcomePage()));
///然后为WelcomePage编写测试代码});
integration_test将用于运行Screen/Page/Widget。这可以用pumpWidget
法来实现。
pumpWidget:从给定的[小部件]呈现UI。所以任何东西都可以在屏幕上绘制和测试。
例子:
testWidgets('Welcome Page Test', (WidgetTester tester) async {
/// Build our app and trigger a frame.
await tester.pumpWidget(const WelcomePage());
///...
});