I'm new to UI Testing and am trying to integrate the feature into an existing project. I'm trying the most basic of tests to just see the framework in action but am having some difficulty. I've added a new UI Testing Bundle target to my project and am able to run my basic test. However I keep getting a "Failure attempting to launch" error that always occurs when I'm trying to actually launch the app in my test. It also says something about a "nil token for current process reference".
Here's the code that I'm testing out:
#implementation SKYPracticeUITesting
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
self.continueAfterFailure = NO;
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication *app = [[XCUIApplication alloc] init];
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
[app launch];
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
NSLog(#"Something is happening");
}
#end
Any help would be greatly appreciated, especially considering the process in Objective-C isn't very well documented.
Related
I have a large Xcode 10 project for an iOS app that has nearly 40 screens and associated view controllers (all are Objective-C). Some of the code is a decade old and some new. The project has no UI or unit tests, but I'd like to enable code coverage and manually exercise the app. Is there a way to add, say, one test that will allow me to run the entire app manually and track which methods and functions are or are not used?
It was simpler than I expected, just not easy because I'd never done any tests before.
I made a UI test that keeps running until some testable event occurs that I can trigger, such as quitting the app (putting it into the background).
Run this test and exercise the app. When done, press Home. The test will exit and the code coverage for whatever you did while running will be shown in the Code Coverage column of the editor.
Add a new target for UI testing.
Edit Scheme, Test, Options: Code Coverage for: all targets.
Add this code as a UI test:
- (void)testCoverage
{
// UI tests must launch the application that they test.
XCUIApplication *app = [[XCUIApplication alloc] init];
[app launch];
// Code Coverage is turned on in the Test scheme
// (a) loop until Home button pushed...
while ( [app waitForState:XCUIApplicationStateRunningBackground timeout:60] == NO ) {
// still in the foreground
};
// now in background
// (b) or, sleep(20);
}
Must be a UI test. As a unit test, the line XCUIApplication *app = [[XCUIApplication alloc] init]; causes an exception.
I'm implementing the performance test for iOS app, and a strange result is derived from it and don't know why.
As Xcode performance test runs given code 10 times, test run on my machine always results such that the first attempt runs in a huge amount of time compared to other 9 attempts.
Jobs that I want to test performance is very simple, it iterates through given array and investigates with a certain conditions. There's no network connection, array manipulation, or reusing of previous variables whatsoever.
Test codes that I wrote is like the following (all of these result in the same behavior).
a. Initialize variables outside the measure block
- (void)testSomething {
// do some initialization
[self measureBlock:^{
// run code to test performance
}];
}
b. Initialize variables inside the measure block
- (void)testSomething {
[self measureBlock:^{
// do some initialization
// run code to test performance
}];
}
c. Initialize variables in -setUp, and use them in test method
- (void)setUp {
// do some initialization here, and capture the pointers as properties
}
- (void)testSomething {
[self measureBlock:^{
// run code to test performance, using properties initialized in setUp
}];
}
- (void)tearDown {
// clean up properties
}
The results are very strange, and the other 9 attempts seem not to be reliable because of the number of first attempt. Why does the first iteration take so much longer?
I am trying to learn automated UI testing with KIF in sample app. My simple test is failing continuously.
My code :
#import <XCTest/XCTest.h>
#import <KIF/KIF.h>
#interface AutomatedUITestsSampleUITests : KIFTestCase
#end
#implementation AutomatedUITestsSampleUITests
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
self.continueAfterFailure = NO;
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
[[[XCUIApplication alloc] init] launch];
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
[tester waitForViewWithAccessibilityLabel:#"LOGIN - Button"];
[tester tapViewWithAccessibilityLabel:#"LOGIN - Button" traits:UIAccessibilityTraitButton];
}
I have set Accessibility Label LOGIN - Button to a UIButton in my storyboard also Accessibility is enabled.
I am getting this in console :
Test Case '-[AutomatedUITestsSampleUITests testExample]' started.
t = 0.00s Start Test at 2017-06-16 13:45:07.071
t = 0.00s Set Up
t = 0.04s Launch com.UITests.AutomatedUITestsSample
t = 4.63s Waiting for accessibility to load
t = 8.77s Wait for app to idle
t = 10.48s Tear Down
Test Case '-[AutomatedUITestsSampleUITests testExample]' failed (10.868 seconds).
Test Suite 'AutomatedUITestsSampleUITests' failed at 2017-06-16 13:45:17.939.
Executed 1 test, with 1 failure (1 unexpected) in 10.868 (12.735) seconds
Test Suite 'AutomatedUITestsSampleUITests.xctest' failed at 2017-06-16 13:45:17.940.
Executed 1 test, with 1 failure (1 unexpected) in 10.868 (12.739) seconds
Test Suite 'All tests' failed at 2017-06-16 13:45:17.941.
Executed 1 test, with 1 failure (1 unexpected) in 10.868 (12.743) seconds
It's a very simple test. Why is it failing? Thanks in advance.
If you have access to the XCUIApplication there is something wrong with your project configuration. KIF tests target should be "Unit Test Target", not "UI Test Target". With correct configuration there is no need to launch the app (and no possibility of app launch manipulation) - it is launched when the unit tests begin. When you use UI test target your tests are running in separate process and KIF doesn't have access to your app.
I suggest to configure your test target again with this guide
When I run a single XCTest class, all tests within succeed.
However when I run it together with other XCTest, some tests in the class fail.
setUp and tearDown method are implemented correctly as following:
- (void)setUp {
[super setUp];
...
}
- (void)tearDown {
...
[super tearDown];
}
I set a breakpoint in the code that should be executed in the test. When I was running the tests in isolation, the breakpoint was reached; when I was running it with other tests, the breakpoint was not reached. I'm thinking that maybe XCTest has some caching behaviors? If so, how to turn it off?
Does anyone know why this might happen?
Thanks a bunch!
I was seeing the same behavior, and my issue was because I had a static variable in my method I was testing that was holding onto its value across tests.
I faced the similar issue when I run all tests together. I could able to fix it by putting Assertion in Dispatch Delay,
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
XCTAssert(self.response != nil)
}
I hope the above solution works for you as well.
I have a test suite in which there are many test classes each one having many tests.
Now if I define
+ (void)tearDown
in any class, then it will be run after all the tests of that class only.
and
- (void)tearDown
will be run after each test.
I want to define a tearDown method and want it to be run after all the tests of all the classes.
Fox eg, if I have 5 classes each having 7 tests each. So I want to run this tearDown function after all the 35 tests.
Since you're looking for a way to call __gcov_flush() after all tests have finished, see https://qualitycoding.org/ios-7-code-coverage/. The latest code from Google Toolbox for Mac shows how to do this using test observers, with versions for both SenTestingKit and XCTest.
If I understand correctly your question, you can take note of completion of each method using a global variable/flag using completion block methods like this:
+ (void)myMethod:(UIView *)exampleView completion:(void (^)(BOOL finished))completion {
if (completion) {
completion(finished);
}
}
Look at this for a better explanation.
Then create a method that checks if all taskes are executed and that runs final test when needed like this:
- (void)finalTest {
if (CHECK GLOBAL FLAG FOR COMPLETION OF OTHER TASKES) {
// do final test
} else {
// repeat check after 0.1 seconds
[self performSelector:#selector(finalTest)
withObject:nil
afterDelay:0.1];
}
}