Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Retrieving Resources for logical unit tests

I've been following Apple's documentation on writing unit tests for the iPhone, and out of the box, the routine doesn't seem to work. I'm not sure I understand where the unit test is going to get the reference to the application delegate.

My Dependencies are like the following: My_Program_target -> UnitTesting_target -> UnitTests_bundle

The following code snippet is where the assert fails. I'm very familiar with CPPUNIT, but I'm having trouble understanding how this crosses over.

- (void) testAppDelegate {      id yourApplicationDelegate = [[UIApplication sharedApplication] delegate];     STAssertNotNil(yourApplicationDelegate, @"UIAppliation failed to find the AppDelegate");  } 

Additionally:

I've decided in my approach to do a logic test. I'm trying to read in an XML file, but I'm not having luck resolving the bundle, which will provide me with the path by which I can access my file. I've tried pasting in the path output by allBundles, but that path doesn't seem to work either. Below is what I'm executing in my test (you can see the debug statement I'm using to output the paths of the bundles):

NSLog(@"BundlePaths: %@", [NSBundle allBundles]);    NSString * path = [[NSBundle bundleWithPath:@"$(TARGET_BUILD_DIR)"] pathForResource:@"SimpleTestList" ofType:@"plist"];    STAssertNotNil(path, @"Bundle Location couldn't find the file specified"); 

Essentially, the assert on path is not successful, but I'm not sure what to put for the path or directory to reference my unitTest bundle that I've told to copy the bundle resources. Calling [NSBundle mainBundle] does not work either.

like image 542
Gary Avatar asked Jul 07 '09 13:07

Gary


People also ask

What is XCTest framework in IOS?

Overview. Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.

How do you come up with unit test cases?

Follow Arrange, Act, Assert The AAA is a general approach to writing more readable unit tests. In the first step, you arrange things up for testing. It's where you set variables, instantiate objects, and do the rest of the required setup for the test to run. During this step, you also define the expected result.

What is XCTAssert in unit testing?

XCTAssert is one of a family of asserts for unit testing from the XCTest framework, and should only be present in Unit Test Targets (i.e. not in your application code). If the assert fails, it does not terminate the execution of the test harness or hosting application, but records and reports the failure.


2 Answers

Ok, so I've figured it out. In order to open a file in a unit test, you'll need to specify the file to open as:

NSString * filePath = [[NSBundle bundleForClass:[self class] ] pathForResource:@"SimpleTestList" ofType:@"plist"]; 

If you include this in a class that's compiled as part of your unit test bundle, that class will look inside the unit test bundle for the file SimpleTestList.plist.

For a unit test, just make sure you set up "Copy Bundle Resources" to include your plist in your unit test bundle.

like image 83
Gary Avatar answered Sep 24 '22 16:09

Gary


If you need the application delegate, you have to run the unit tests on the device itself and not the simulator. Also, you will see unit test output appear in the console, not in the build results.

The key thing to know is that there are two types of unit tests - logic tests that are run outside of the executable, and then integrated system kinds of tests that need the full running environment.

The logic tests MUST be run with the simulator selected as the target or they will not run.

The integrated system tests MUST be run as part of the executable, on the device - you'll want a new target to accomplish this.

Sorry this is all so complex, this aspect is still very much a work in progress compared to many other unit testing frameworks.

like image 25
Kendall Helmstetter Gelner Avatar answered Sep 20 '22 16:09

Kendall Helmstetter Gelner