I can't delete my application during the teardown phase of my tests
You can reset application's permissions with Xcode 11.4 API
https://developer.apple.com/documentation/xctest/xcuiapplication/3526066-resetauthorizationstatus
If you still want to delete the app on iOS 13, check my answer here
How to delete/reset an app from iOS 13 with XCTest?
Accessing App Delegate from XCTestCase Subclass - Wrong Type?
Actually there is a single App Delegate file in the project.But when you add AppDelegate.swift to the tests target members,then your Test's App Delegate Class will be different than the AppDelegate class.
if let delegate = UIApplication.sharedApplication().delegate{
The above If
statement tries to returns project delegate instance different than you are trying to Cast.
if let myDelegate = delegate as? AppDelegate
And hence casting to AppDelegate
doesnot succeed.So,you get always fail as
XCTFail("Application Delegate Object Is of The Wrong Type")
UPDATED:
i wrote some few test cases to know the exact problem:
1)Test Case to find out if AppDelegate is nil. (Passes)
if let delegate = UIApplication.sharedApplication().delegate{
XCTAssertNotNil(delegate, "not nil")
2)So,next test is below (Fails)
XCTAssertNotNil(delegate as? AppDelegate, "not nil")
This test case fails..So its sure there was problem in casting.
How do I reset the application data after each test with Xcode 7 UI Testing?
Not in a straight forward manner. But there are some workarounds.
The XCUIApplication
can set command line arguments and environment variables that can alter your application’s behavior.
A simple example of your main.m
file:
int main(int argc, char * argv[]) {
#if DEBUG
// Reset all data for UI Testing
@autoreleasepool {
for (int i = 1; i < argc; ++i) {
if (0 == strcmp("--reset-container", argv[i])) {
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSFileManager *fm = [[NSFileManager alloc] init];
for (NSString *path in folders) {
[fm removeItemAtPath:path error:nil];
}
// Also remove documents folder if necessary...
}
}
}
#endif
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
}
}
And in -[XCTestCase setUp]
add:
XCUIApplication *app = [[XCUIApplication alloc] init];
app.launchArguments = @[@"--reset-container"];
[app launch];
Unable to access Main Target Methods when Adding Tests to OSX Project in Swift
I managed to get it working by adding Testable to the top of the class( This appears to be OSX specific issue)
import XCTest
@testable import MyProjectName // <--- This was the missing bit.... :)
class MyProject_DesktopTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// The Test would go in here but I can't seem to resolve EmailHelper class- it generates a lint error
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
// Put the code you want to measure the time of here.
}
}
}
Also be sure to clean your project after adding it an it seems to work.
Related Topics
Interrupted Purchase Not Calling Delegate After Accepting T&C
A Method Without Parameters Is Calling for an Argument
Different Path Url for Filemanager Everytime I Open the App
Url Constructor Doesn't Work with Some Characters
Unrecognized Selector Sent to Instance When No Related Entities Found in Core Data
How to Pass Closure as a Parameter in Perform(Selector, Withobject)
Core Data Update in Swift While Selecting Any Row in List Table View Not Working
Retrieve an Image from Firebase to an Uiimage Swift5
What Does the Snapshot/Observer Code Do in Firebase
Swift MACos Popover Detect Change Dark Mode
How to Check for Value in Firebase That Is Held Under a Autoid Child
Swiftui: Problem with Adding a Property to Data Struct
Receiving Data from Nsinputstream in Swift
Check If on Correct Dispatch Queue in Swift 3