Skip to content

Unit Testing

Eric Skroch edited this page Feb 27, 2015 · 2 revisions

Introduction

Because Core Plot is intended to be used in scientific, financial and other domains where correctness is paramount, unit testing is integrated into the framework. Good test coverage protects developers from introducing accidental regressions and frees them to experiment and refactor without fear of breaking things.

To these ends, we encourage developers to add unit tests for all non-trivial code changes. In addition, we hope that you will add a sample of any new features to the Plot Gallery or another example app to both demonstrate the new functionality you have contributed, and to provide sample code for its use.

We make use of the XCTest testing framework that is bundled with Xcode. For a general introduction to the XCTest framework, read Apple's "Testing With Xcode" manual. Read on below for information on writing unit tests specifically for Core Plot.

Writing a new unit test module

All unit test modules should inherit from CPTTestCase. CPTTestCase is a XCTestCase subclass that handles setting the output folder for rendering/state test output (see below). Modules testing Core Plot classes that require a data source may wish to subclass CPTDataSourceTestCase instead of CPTTestCase. CPTDataSourceTestCase provides an implementation of the plot data source protocol.

To create a new test module:

  1. Select "New File..." in Xcode and add a file using the "Objective-C test case" Xcode template. In Core Plot, test modules are named after the module they test, with "Tests" appended to the name. For example the tests for CPTTextLayer are in CPTTextLayerTests.h/.m.

  2. Add the new test module's .h/.m files to the CorePlot-UnitTests target.

  3. Move the new test module's .h/.m files to the "Tests" group under the group containing the module they test in the Xcode Groups & Files tree. Create a new Tests group if none exists.

  4. Modify the .h of your test module to match this template:

#import "CPTTestCase.h"

@interface CPT[Module]Tests : CPTTestCase {

}

@end

Writing unit tests

The XCTest framework test runner will automatically run all test methods beginning with test, so the following template is a good start for writing a test method:

@implementation CPT[Module]Tests

-(void)setUp
{
    // this method runs before each test in this class
}

-(void)tearDown
{
    // this method runs after each test in this class
}

- (void)testBlah
{
    XCTFail(@"This test has not been implemented yet.");
}

@end

Obviously, you'll want to replace the XCTFail macro with your test code. The -setUp and -tearDown methods are useful for setting up and removing dependencies required for every test in the test class. They are optional and can be omitted if not required.