You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NickyWeber edited this page Sep 26, 2014
·
4 revisions
Getting started with (Unit) Testing in SpriteBuilder
Creating Tests
You should put test cases in the SpriteBuilder Tests folder.
Try to use the same group structure as in the SpriteBuilder folder, see screenshots.
Folder structure of code to be tested
Folder structure for tests
Tests in SpriteBuilder use the XCTest framework. You can easily create new test cases by using the XCode template.
Give the test a name following the pattern: <ClassName>_Tests
Like in the example above, the class MoveFileCommand would get a test called MoveFileCommand_Tests.
Make sure the test case is in the SpriteBuilder Tests target only
Basics
You can run tests in XCode hitting ⌘U or using the toolbar button, just click hold and it'll show a dropdown, choose Test.
Every test case is a single file class inheriting from XCTestCase. To add tests, just add methods prefixed with test, like testAwesomeFireworksIlluminatingTheNightSky. Try to give the test a name that describes what it is doing. XCode will run all methods starting with test.
There are two methods of importance, this code is generated for you by the template:
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
If you don't have setUp or tearDown code you can remove the methods.
Every test method should contain at least one XCTAssertABC statement, otherwise it'll just pass. Some documentation on the assert macros. Please make use of the format... (Like NSLog()) parameter to add more helpful information about a failing test. A failing tests means reading code and possibly debugging, every piece of information can help to figure out why a test failed.
In the folder TestHelpers you can find some useful stuff.
SBAsserts.h hosts SBAssertStringsEqual to easily compare strings and prints some info on the console
ObserverTestHelper is meant to reduce boilerplate code for testing notification with OCMock
FileSystemTestCase extends XCTestCase to provide an environment for writing tests that involves the file system
Resource Files
More advanced tests can benefit from pre-generated files, e.g. a sound file. To make use of those files you can add them to the Resources group, make sure those are in the copy bundle resources phase of the SpriteBuilder Test target. The test can then take a hold on those files with Apple's NSBundle class.
If you got code that should not be tested within a class, like an alert being shown, exclude the code by wrapping it in a
#ifndef TESTING
// Some code not to be compiled for test target
#endif
Preprocessor Macros
If there is need for more preprocessor macros defined at a target level, add those to the SpriteBuilder target, not the SpriteBuilder Tests target. There is a special Build Configuration Testing for this purpose.
AppCode
AppCode users need to add a Run Configuration to run tests. Main Menu -> Run -> Edit Configurations. Hit the + button and select XCTest/Kiwi, give it a name. Select Target and Configuration like in the screenshot. Unlike XCode (⌘U) you have to select a configuration first to run tests.