Skip to content

Refactor iOS integration_test API to support Swift, dynamically add native tests#88013

Merged
fluttergithubbot merged 4 commits into
flutter:masterfrom
jmagman:flat-integration-test-api
Aug 17, 2021
Merged

Refactor iOS integration_test API to support Swift, dynamically add native tests#88013
fluttergithubbot merged 4 commits into
flutter:masterfrom
jmagman:flat-integration-test-api

Conversation

@jmagman

@jmagman jmagman commented Aug 11, 2021

Copy link
Copy Markdown
Member

Change the iOS native integration_tests to run the dart tests, and dynamically add the test results so that each dart test appears as a native iOS test.

  • Add a FLTIntegrationTestCase to become the XCTestCase base class, removing the need for the Swift-incompatible INTEGRATION_TEST_IOS_RUNNER macro. This class swizzles a new class instance method per completed dart test.
  • Create a screenshot placeholder test for any captureScreenshot requests over the channel. Unfortunately there's no mapping to know which test it came from (yet) so they are all grouped together.
  • Replace FLTIntegrationTestScreenshotDelegate with the property IntegrationTestPlugin.capturedScreenshotsByName. I was seeing issues where only the first FLTIntegrationTestCase was able to see the screenshots because new FLTIntegrationTestRunner delegates were being created every time. Instead, store it on the static +[IntegrationTestPlugin instance] so it's accessible from different FLTIntegrationTestRunners.
  • Add a RunnerSwiftTests.swift test file to prove it works in a Swift test.
  • Update the README.

Success (note there are three instances of the tests because I created FLTIntegrationTestCase to test different scenarios):
Screen Shot 2021-08-10 at 7 15 32 PM

Failure:
Screen Shot 2021-08-10 at 7 16 45 PM

Next I need to actually run the integration test during CI: #87770

Fixes #71873

@jmagman jmagman added platform-ios iOS applications specifically f: integration_test The flutter/packages/integration_test plugin labels Aug 11, 2021
@jmagman jmagman self-assigned this Aug 11, 2021
@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@google-cla google-cla Bot added the cla: yes label Aug 11, 2021
@jmagman
jmagman force-pushed the flat-integration-test-api branch from fa301a9 to 090b9b0 Compare August 11, 2021 02:26
@jmagman jmagman changed the title Refactor iOS integration_test API to support Swift Refactor iOS integration_test API to support Swift, dynamically add native tests Aug 11, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These build settings were automatically changed when I added a Swift file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include the RunnerTests target during analysis.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated and moved from IntegrationTestIosTest.h

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FLTIntegrationTestCase only works from a XCTest target (not from the app itself).

@jmagman
jmagman force-pushed the flat-integration-test-api branch from 090b9b0 to 0d2b4c9 Compare August 11, 2021 02:33
@jmagman

jmagman commented Aug 11, 2021

Copy link
Copy Markdown
Member Author

@stuartmorgan This refactor was larger than I was expecting... #84611 (comment)

@jmagman

jmagman commented Aug 11, 2021

Copy link
Copy Markdown
Member Author

\cc @cbracken this shouldn't be too bad to also implement for macOS. The only rough edge I can think of is UIImage instead of NSImage.

@stuartmorgan-g stuartmorgan-g left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slick! Out of curiosity, do we know if it's guaranteed to work? It seems a bit modifying-an-iterator-while-iterating, which makes me wonder if it has defined behavior.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually the purpose of + (NSArray<NSInvocation *> *)testInvocations--a hook for dynamically changing the test methods to use for a test case. https://stackoverflow.com/a/55204082 was a helpful reference.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we could fix the casing of "IOS" while we're here, or is that breaking to a public API?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, and there's nothing iOS-y about it other than the UIImage. I'll deprecate the entire IntegrationTestIosTest instead of just testIntegrationTest: and replace it with a FLTIntegrationTestRunner class. It would be the same result if anyone was using this API (which the docs don't say to do, but it was technically possible).

@jmagman
jmagman force-pushed the flat-integration-test-api branch from 0d2b4c9 to 2488f62 Compare August 16, 2021 22:33
// Test deprecated macro. Do not use.
INTEGRATION_TEST_IOS_RUNNER(RunnerObjCMacroTests)

@interface DeprecatedIntegrationTestIosTests : XCTestCase

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added this, validates the deprecated IntegrationTestIosTest still works:

Test Suite 'DeprecatedIntegrationTestIosTests' started at 2021-08-16 15:40:22.353
Test Case '-[DeprecatedIntegrationTestIosTests testIntegrationTest]' started.
2021-08-16 15:40:22.355741-0700 Runner[87799:71056731] ==================== Test Results =====================
2021-08-16 15:40:22.355888-0700 Runner[87799:71056731] verify text passed.
2021-08-16 15:40:22.355992-0700 Runner[87799:71056731] verify screenshot passed.
2021-08-16 15:40:22.356089-0700 Runner[87799:71056731] ================== Test Results End ====================
Test Case '-[DeprecatedIntegrationTestIosTests testIntegrationTest]' passed (0.004 seconds).

@stuartmorgan-g stuartmorgan-g left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fluttergithubbot
fluttergithubbot merged commit 9e3de9a into flutter:master Aug 17, 2021
@jmagman
jmagman deleted the flat-integration-test-api branch August 17, 2021 18:42
zanderso added a commit that referenced this pull request Aug 17, 2021
jmagman pushed a commit that referenced this pull request Aug 17, 2021
blasten pushed a commit to blasten/flutter that referenced this pull request Aug 19, 2021
blasten pushed a commit to blasten/flutter that referenced this pull request Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

f: integration_test The flutter/packages/integration_test plugin platform-ios iOS applications specifically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ios] [integration_test] avoid using preprocessor macros to make it easier to use from Swift

3 participants