XCTest: No public way to check whether a test case is running as a UI test
| Originator: | apsharp | ||
| Number: | rdar://23329276 | Date Originated: | 30-Oct-2015 06:38 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 7.1 (7B91b) |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
In the context of a project with UI tests, it's implicitly clear that a test case is running in the context of a UI test. However in the context of a more general testing library that provides an XCTestCase subclass, test cases on that class may end up being executed in different contexts (i.e., when executing the unit tests for that library).
If you try to instantiate an XCUIApplication instance outside of a UI testing context, an exception is raised. It would be very useful to expose publicly the check for whether we're UI testing, to allow code to safely avoid instantiating an XCUIApplication object.
Steps to Reproduce:
1. Compile the following test case as part of a framework:
class AutomaticallyTerminatesAfterRunning: XCTestCase {
override func tearDown() {
super.tearDown()
XCUIApplication.terminate()
}
}
2. Run that framework's unit tests. An exception is raised when instantiating the XCUIApplication instance.
3. Link that framework into a UI testing bundle, and derive a test case from it. It will not crash, and the application will be automatically terminated after each test case runs.
Expected Results:
N/A
Actual Results:
N/A
Regression:
N/A
Notes:
Take a look at this PR for an example (including a workaround that's a little ugly): https://github.com/sharplet/Scenarios/pull/17
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!