A light-weight TDD / BDD framework for Objective-C.
- An Objective-C RSpec-like BDD DSL
- Quick and easy set up
- Built on top of XCTest
- Excellent Xcode integration
#import <Specta/Specta.h> // #import "Specta.h" if you're using libSpecta.a
SharedExamplesBegin(MySharedExamples)
// Global shared examples are shared across all spec files.
sharedExamplesFor(@"foo", ^(NSDictionary *data) {
__block id bar = nil;
beforeEach(^{
bar = data[@"bar"];
});
it(@"should not be nil", ^{
XCTAssertNotNil(bar);
});
});
SharedExamplesEnd
SpecBegin(Thing)
describe(@"Thing", ^{
sharedExamplesFor(@"another shared behavior", ^(NSDictionary *data) {
// Locally defined shared examples can override global shared examples within its scope.
});
beforeAll(^{
// This is run once and only once before all of the examples
// in this group and before any beforeEach blocks.
});
beforeEach(^{
// This is run before each example.
});
it(@"should do stuff", ^{
// This is an example block. Place your assertions here.
});
it(@"should do some stuff asynchronously", ^{
waitUntil(^(DoneCallback done) {
// Async example blocks need to invoke done() callback.
done();
});
});
itShouldBehaveLike(@"a shared behavior", @{@"key" : @"obj"});
itShouldBehaveLike(@"another shared behavior", ^{
// Use a block that returns a dictionary if you need the context to be evaluated lazily,
// e.g. to use an object prepared in a beforeEach block.
return @{@"key" : @"obj"};
});
describe(@"Nested examples", ^{
it(@"should do even more stuff", ^{
// ...
});
});
pending(@"pending example");
pending(@"another pending example", ^{
// ...
});
afterEach(^{
// This is run after each example.
});
afterAll(^{
// This is run once and only once after all of the examples
// in this group and after any afterEach blocks.
});
});
SpecEndbeforeEachandafterEachare also aliased asbeforeandafterrespectively.describeis also aliased ascontext.itis also aliased asexampleandspecify.itShouldBehaveLikeis also aliased asitBehavesLike.- Use
pendingor prependxtodescribe,context,example,it, andspecifyto mark examples or groups as pending. - Use
^(DoneCallback done)as shown in the example above to make examples wait for completion.done()callback needs to be invoked to let Specta know that your test is complete. The default timeout is 10.0 seconds but this can be changed by calling the functionsetAsyncSpecTimeout(NSTimeInterval timeout). (before|after)(Each/All)also accept^(DoneCallback done)s.- Do
#define SPT_CEDAR_SYNTAXbefore importing Specta if you prefer to writeSPEC_BEGINandSPEC_ENDinstead ofSpecBeginandSpecEnd. - Prepend
fto yourdescribe,context,example,it, andspecifyto set focus on examples or groups. When specs are focused, all unfocused specs are skipped. - To use original XCTest reporter, set an environment variable named
SPECTA_REPORTER_CLASStoSPTXCTestReporterin your test scheme. - Set an environment variable
SPECTA_SHUFFLEwith value1to enable test shuffling. - Set an environment variable
SPECTA_SEEDto specify the random seed for test shuffling.
Standard XCTest matchers such as XCTAssertEqualObjects and XCTAssertNil work, but you probably want to add a nicer matcher framework - Expecta to your setup. Or if you really prefer, OCHamcrest works fine too. Also, add a mocking framework: OCMock.
Specta is considered a done project, there are no plans for active development on the project at the moment aside from ensuring future Xcode compatability. Therefore it is a stable dependency, but will not be moving into the Swift world. If you are looking for that, we recommend you consider Quick.
- Run
rake testin the cloned folder.
- Please use only spaces and indent 2 spaces at a time.
- Please prefix instance variable names with a single underscore (
_). - Please prefix custom classes and functions defined in the global scope with
SPT.
Use CocoaPods, Carthage or Set up manually
- Add Specta to your project's
Podfile:
target :MyApp do
# your app dependencies
target :MyAppTests do
inherit! :search_paths
pod 'Specta', '~> 2.0'
# pod 'Expecta', '~> 1.0' # expecta matchers
# pod 'OCMock', '~> 2.2' # OCMock
# pod 'OCHamcrest', '~> 3.0' # hamcrest matchers
# pod 'OCMockito', '~> 1.0' # OCMock
# pod 'LRMocky', '~> 0.9' # LRMocky
end
end- Run
pod installin your project directory.
-
Add Specta to your project's
Cartfile.privategithub "specta/specta" ~> 2.0 -
Run
carthage updatein your project directory -
Drag the appropriate
Specta.frameworkfor your platform (located in Carthage/Build/) into your application’s Xcode project, and add it to your test target(s). -
If you are building for iOS, a new
Run Script Phasemust be added to copy the framework. The instructions can be found on Carthage's getting started instructions
- Clone from GitHub.
- Run
rakein project root to build. - Add a "Cocoa/Cocoa Touch Unit Testing Bundle" target if you don't already have one.
- Copy and add all header files in
Productsfolder to the Test target in your Xcode project. - For OS X projects, copy and add
Specta.frameworkinProducts/osxfolder to the test target in your Xcode project. For iOS projects, copy and addSpecta.frameworkinProducts/iosfolder to the test target in your Xcode project. You can alternatively uselibSpecta.a, if you prefer to add it as a static library for your project. (iOS 7 and below require this) - Add
-ObjCand-all_loadto the "Other Linker Flags" build setting for the test target in your Xcode project. - If you encounter linking issues with
_llvm_*symbols, ensure your target's "Generate Test Coverage Files" and "Instrument Program Flow" build settings are set toYes.
Copyright (c) 2012-2022 Specta Team. This software is licensed under the MIT License.