是否可能在Firebase上运行Flutter集成测试?关于这个问题似乎存在一些矛盾的信息 - 有些来源说可以,但是在文档中,iOS部分似乎只能在自己的设备上进行测试。
Firebase要求上传一个XCT测试包。如何为Flutter项目获取/创建它?
是否可能在Firebase上运行Flutter集成测试?关于这个问题似乎存在一些矛盾的信息 - 有些来源说可以,但是在文档中,iOS部分似乎只能在自己的设备上进行测试。
Firebase要求上传一个XCT测试包。如何为Flutter项目获取/创建它?
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import '../lib/main.dart' as app;
// How to run?
// flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
// Currently, we can't setup intergration tests, because there is a issue with integraiton test package +
// localization.
//
// See this ticket for information: https://github.com/flutter/flutter/issues/84053
testWidgets('full app test', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.pump();
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
ios/Runner.xcworkspace
。如果您还没有测试目标,请通过 文件 > 新建 > 目标...
并选择 Unit Testing Bundle
创建一个测试目标。将 Product Name
更改为 RunnerTests
。确保 Target to be Tested
设置为 Runner
,语言设置为 Objective-C
。选择 完成
。
确保在 构建设置 部分中 RunnerTests
的 iOS Deployment Target 与 Runner
相同。Runner
目标中,将新的测试目标添加到 ios/Podfile
中。target 'Runner' do
# Do not change existing lines.
...
target 'RunnerTests' do
inherit! :search_paths
end
end
要从命令行构建integration_test/foo_test.dart
,请运行:
flutter build ios --config-only integration_test/foo_test.dart
RunnerTests.m
(或任何你选择的名称)的测试文件到新目标,并替换该文件。@import XCTest;
@import integration_test;
INTEGRATION_TEST_IOS_RUNNER(RunnerTests)
运行Product > Test
在您选择的设备上运行集成测试。
要将其部署到Firebase Test Lab,您可以按照以下步骤操作:
在Flutter应用程序根目录执行此脚本:
output="../build/ios_integ"
product="build/ios_integ/Build/Products"
dev_target="14.3"
# Pass --simulator if building for the simulator.
flutter build ios integration_test/foo_test.dart --release
pushd ios
xcodebuild -workspace Runner.xcworkspace -scheme Runner -config Flutter/Release.xcconfig -derivedDataPath $output -sdk iphoneos build-for-testing
popd
pushd $product
zip -r "ios_tests.zip" "Release-iphoneos" "Runner_iphoneos$dev_target-arm64.xctestrun"
popd
xcodebuild test-without-building -xctestrun "build/ios_integ/Build/Products/Runner_iphoneos14.3-arm64.xctestrun" -destination id=<YOUR_DEVICE_ID>
一切正常后,您可以将生成的zip上传到Firebase测试实验室(将模型更改为您的值):
gcloud firebase test ios run --test "build/ios_integ/ios_tests.zip" --device model=iphone11pro,version=14.1,locale=fr_FR,orientation=portrait
注意:我从 https://github.com/flutter/flutter/tree/master/packages/integration_test#ios-device-testing 复制了必要的iOS设备测试步骤(感谢Flutter团队 ❤️)
如果您想在Android Firebase测试实验室上运行这些测试,还需要进行一些额外的步骤。 您将在以下位置找到这些步骤:https://github.com/flutter/flutter/tree/master/packages/integration_test#android-device-testing。 我还建议您查看这个视频,其中Reso Coder解释并展示了每一个小步骤:https://www.youtube.com/watch?v=izajHHFSa8o
首先在Xcode中打开你的iOS模块,如下所示。
现在你可以使用Swift编写酷炫的XCode UI测试。例如,我在这里放置了一个Firebase登录测试,供你的应用程序使用。
import XCTest
import UIKit
import Firebase
class LoginTestUITests: XCTestCase {
var app: XCUIApplication!
func testLogin() {
continueAfterFailure = false
app = XCUIApplication()
app.launch()
wait(for: 2)
passLogin()
wait(for: 5)
// after your login you can add some methods here according to your app flow
}
}
extension LoginTestUITests {
func passLogin() {
//put your logic here
}
// this is a pretty cool extension waiting according to input for a while coming the data from backend etc.
extension XCTestCase {
func wait(for duration: TimeInterval) {
let waitExpectation = expectation(description: "Waiting")
let when = DispatchTime.now() + duration
DispatchQueue.main.asyncAfter(deadline: when) {
waitExpectation.fulfill()
}
// I use a buffer here to avoid flakiness with Timer on CI
waitForExpectations(timeout: duration + 0.5)
}
}
#import <XCTest/XCTest.h>
#import <integration_test/IntegrationTestIosTest.h>
INTEGRATION_TEST_IOS_RUNNER(RunnerTests)