简体   繁体   English

Flutter:调试集成测试

[英]Flutter: Debugging an integration test

I'm learning Flutter and using Android Studio as my IDE and i've hit some pain points around Integration Testing.我正在学习 Flutter 并使用 Android Studio 作为我的 IDE,我在集成测试方面遇到了一些痛点。

As part of the learning process i have written a basic Integration Test.作为学习过程的一部分,我编写了一个基本的集成测试。

The intention with this integration test is to examine an Image widget (created via Image.Asset) to see if the image source, which is dynamically generated, is the expected value, or that an image is being displayed.此集成测试的目的是检查图像小部件(通过 Image.Asset 创建)以查看动态生成的图像源是否为预期值,或者是否正在显示图像。

I run the integration test using the Terminal tab in the IDE, eg: flutter drive --target=my_app/test_driver/user_list_scrolling.dart我使用 IDE 中的终端选项卡运行集成测试,例如: flutter drive --target=my_app/test_driver/user_list_scrolling.dart

I want to add a breakpoint to my Integration Test method and step through it from within Android Studio to help aid my learning of the testing functions.我想在我的集成测试方法中添加一个断点,并从 Android Studio 中逐步执行它,以帮助我学习测试功能。

My questions are:我的问题是:

How can i debug an integration test from within Android Studio?如何从 Android Studio 中调试集成测试? - As I'm learning i would love to put a breakpoint in my integration test and play around with the Finders in the immediate window. However, when i start my integration test from the terminal my Breakpoints seem to be ignored, i also tried adding the Debugger(); - 在我学习的过程中,我很乐意在我的集成测试中放置一个断点,并立即在 window 中使用 Finders。但是,当我从终端开始我的集成测试时,我的断点似乎被忽略了,我也尝试添加Debugger(); command.命令。 Execution paused, but i was unable to step through my code in Android Studio.执行暂停,但我无法单步执行 Android Studio 中的代码。 I've also tried using the 'Attach to process' option in the IDE but the 'Choose process' list is empty.我还尝试使用 IDE 中的“附加到进程”选项,但“选择进程”列表为空。

Can i execute an integration test from within Android Studio without having to manually enter a command into a terminal?我可以在 Android Studio 中执行集成测试,而无需在终端中手动输入命令吗? - i would rather click a button than memorise a command. - 我宁愿点击一个按钮也不愿记住一个命令。 Right-clicking my integration test file and selecting run does not appear to work.右键单击我的集成测试文件并选择运行似乎不起作用。

How can i effectively test an Image widget from within an Integration Test?我如何从集成测试中有效地测试图像小部件? - The image source is set by calling Image.Asset() with a calculated value as the first argument, so i want to confirm that an image is displayed / the argument is the expected value. - 图像源是通过将计算值作为第一个参数调用 Image.Asset() 来设置的,因此我想确认显示图像/参数是预期值。 I'm guessing i need to use find.byType("Image") and somehow examine the result for the source value?我猜我需要使用find.byType("Image")并以某种方式检查源值的结果?

With the new integration_test package you can just run flutter run integartion_test/app_test.dart to debug your tests.使用新的integration_test package,您只需运行flutter run integartion_test/app_test.dart即可调试您的测试。

My launch.json (I'm using VS Code):我的launch.json (我使用的是 VS 代码):

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Integration Test",
      "request": "launch",
      "type": "dart",
      "program": "example/integration_test/app_test.dart",
    },
  ]
}

VS 代码调试器

The following are the steps I took to set-up for integration test development using Flutter tooling, including debugging: 以下是我使用Flutter工具设置集成测试开发所采取的步骤,包括调试:

  1. Configure the app to listen on a shared port (in this case 8888) 配置应用程序以侦听共享端口(在本例中为8888) 将' -  observatory-port 8888'添加到'附加参数' Add '— observatory-port 8888' to 'Additional Arguments' 将' - observatory-port 8888'添加到'附加参数'

  2. Configure integration test to connect on the same shared port 配置集成测试以在同一共享端口上进行连接 在此输入图像描述 Add 'VM_SERVICE_URL= http://127.0.0.1:8888/ ' to 'Environment Variables' 将'VM_SERVICE_URL = http://127.0.0.1:8888/ '添加到'环境变量'

  3. Start the app in run or debug-mode (only required once, with hot-reload when needed): 在运行或调试模式下启动应用程序(仅需要一次,需要时进行热重新加载): 在此输入图像描述

  4. Start the integration test in run or debug mode (as many times as you want): 在运行或调试模式下启动集成测试(根据需要多次): 在此输入图像描述

You can now add breakpoints to the app and/or test and view source code and variables in debugger. 您现在可以向应用程序添加断点和/或在调试器中测试和查看源代码和变量。

The following describes how to setup Android Studio to develop integration tests in more detail. 下面介绍如何设置Android Studio以更详细地开发集成测试。

A how-to for fast integration test development with existing tooling 使用现有工具进行快速集成测试开发的方法

For some reason in Android Studio the icon to run in debug mode doesn't work with configurations scoped to entire directories.出于某种原因,在 Android Studio 中,在调试模式下运行的图标不适用于范围为整个目录的配置。 Create a configuration targeting one file, or simply click the "Run Test" icon in the gutter next to your main() function and select the "Debug" option.创建一个针对一个文件的配置,或者只需单击main()函数旁边装订线中的“运行测试”图标,然后选择“调试”选项。

右键单击装订线中的测试运行按钮或输入键盘快捷键

You can run then the integration test from the AndroidStudio as you would run any other Flutter app (right-click on the file, run -> debug).然后,您可以像运行任何其他 Flutter 应用程序一样从 AndroidStudio 运行集成测试(右键单击该文件,运行 -> 调试)。

Sometimes the AndroidStudio identifies the file as Dart and not Flutter, so you have to create a new Run/Debug configuration for Flutter instead.有时 AndroidStudio 将文件识别为 Dart 而不是 Flutter,因此您必须为 Flutter 创建一个新的运行/调试配置。 At "Dart entrypoint", select the integration file with the "main" func.在“Dart 入口点”,select 是带有“main”函数的集成文件。

在此处输入图像描述

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

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