Firebase上iOS Flutter集成测试?

12

是否可能在Firebase上运行Flutter集成测试?关于这个问题似乎存在一些矛盾的信息 - 有些来源说可以,但是在文档中,iOS部分似乎只能在自己的设备上进行测试。

Firebase要求上传一个XCT测试包。如何为Flutter项目获取/创建它?


我不明白你确切想要什么? - Janvi Patel
Firebase要求上传一个XCT测试包。如何为Flutter项目获取/创建它? - Ali
4个回答

3
是的,您可以在Firebase Test Lab上为iOS编写Flutter集成测试。即使使用Dart代码而不是Swift代码也可以。您需要使用“integration_test”包。您可以在Flutter文档中找到如何设置“integration_test”包的说明:https://flutter.dev/docs/testing/integration-tests 您的测试将类似于以下内容:
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);
  });
}

为了能够将此测试作为 XCTest 运行(这是 Firebase Test Lab 需要的),您需要执行一些额外步骤:
在 Xcode 中打开 ios/Runner.xcworkspace。如果您还没有测试目标,请通过 文件 > 新建 > 目标... 并选择 Unit Testing Bundle 创建一个测试目标。将 Product Name 更改为 RunnerTests。确保 Target to be Tested 设置为 Runner,语言设置为 Objective-C。选择 完成。 确保在 构建设置 部分中 RunnerTestsiOS Deployment TargetRunner 相同。
通过将其嵌入到现有的 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

在Xcode中,添加一个名为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


1
在 Firebase 测试实验室上执行测试失败,错误为:XCTest zip 文件格式不正确。该 zip 文件不包含单个 .xctestrun 文件和 DerivedData/Build/Products 目录的内容。 - Taba

2

首先在Xcode中打开你的iOS模块,如下所示。

enter image description here

现在你可以使用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)
    }
}

还有一个很好的示例案例,说明如何在不同屏幕上编写滑动测试。https://www.swiftbysundell.com/articles/getting-started-with-xcode-ui-testing-in-swift/ - zeytin
那么没有办法用Dart / Flutter编写测试并将其用于吗?我猜这些Swift测试无法与使用Flutter创建的按钮/ UI元素交互。 - Ali
我认为你需要实现Swifty代码。 - zeytin

1
打开Xcode项目(默认为ios/Runner.xcodeproj)。创建一个测试目标(导航到文件>新建>目标...并设置值)和一个测试文件RunnerTests.m,并更改代码。您可以将RunnerTests.m更改为您选择的名称。
#import <XCTest/XCTest.h>
#import <integration_test/IntegrationTestIosTest.h>

INTEGRATION_TEST_IOS_RUNNER(RunnerTests)

这是与文档中相同的信息,但它没有解释:1)这是否会运行我的Flutter集成测试?2)我如何获取Firebase要求的XCT包? - Ali

0

你需要有Xcode才能做这件事。如果你正在使用Android,请先在Xcode中打开iOS模块。

enter image description here

接下来在Xcode中打开测试导航器(Command-6),并添加新的测试目标: enter image description here

现在我们在Flutter项目中有两个用于单元测试和UI测试的目标。

enter image description here

我们的项目已经准备好进行测试,可以通过在Xcode中选择Product ▸ Test或按下Command-U来运行它:

enter image description here

接下来是 Firebase 测试实验室:

  • iOS:编写 XCTests,然后构建并打包您的应用以进行上传。

只需使用 Xcode Product > Archive 创建应用程序包,选择真实设备作为目标即可。


1
我必须用Swift编写测试吗?还是有一种方法可以使用Flutter代码在Dart中编写我的测试,并以某种方式将其连接到XCTests,以便Firebase可以运行? - Ali
1
看起来需要用Swift编写,没有找到Dart的相关内容,但这应该不是很难。这里有一个链接可以帮助你:https://www.raywenderlich.com/960290-ios-unit-testing-and-ui-testing-tutorial ,祝好运! - Sergei Sevriugin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接