Specta

一个轻量级的 TDD/BDD 框架,用于 Objective-C & Cocoa。(A light-weight TDD / BDD framework for Objective-C & Cocoa)

Github星跟蹤圖

Specta

Objective-C 的轻量级 TDD/BDD 框架。

特性

  • Objective-C RSpec-like BDD DSL
  • 快速简便的设置
  • 建立在 XCTest 之上
  • 出色的 Xcode 集成

屏幕截图

(恕删略)

实例

#import <Specta/Specta.h> // #import "Specta.h" 如果您使用 libSpecta.a
SharedExamplesBegin(MySharedExamples)
// 全局共享示例在所有spec文件中共享。
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) {
    // 本地定义的共享示例可以在其范围内覆盖全局共享示例。
  });
  beforeAll(^{
    // 在本组所有示例之前以及在每个 beforeEach 块之前运行一次,
    // 且仅运行一次。
  });
  beforeEach(^{
    // 这是在每个示例之前运行的。
  });
  it(@"should do stuff", ^{
    // 这是一个示例块。 将断言放在这里。
  });
  it(@"should do some stuff asynchronously", ^{
    waitUntil(^(DoneCallback done) {
      // 异步示例块需要调用 done() 回调。
      done();
    });
  });
  itShouldBehaveLike(@"a shared behavior", @{@"key" : @"obj"});
  itShouldBehaveLike(@"another shared behavior", ^{
    // 如果需要延迟计算上下文,可以使用返回字典的块
    // 例如:使用在 beforeEach 块中准备的对象。
    return @{@"key" : @"obj"};
  });
  describe(@"Nested examples", ^{
    it(@"should do even more stuff", ^{
      // ...
    });
  });
  pending(@"pending example");
  pending(@"another pending example", ^{
    // ...
  });
  afterEach(^{
    // 这是在每个示例之后运行的。
  });
  afterAll(^{
    // 在本组的所有示例之后,以及在每个 afterEach 块之后,
    // 只运行一次。
  });
});
SpecEnd
  • beforeEachafterEach 也分别作为 beforeafter 别名。
  • describe 也作为 context 别名。
  • it 也别名为 examplespecify.
  • itShouldBehaveLike 也别名为 itBehavesLike.
  • 使用 pending 或在前面加上 xdescribe, context, example, it, 并specify 将示例或组标记为挂起。
  • 使用 ^(DoneCallback done) ,如上例所示,使示例等待完成。需要调用 done() 回调,让Specta知道你的测试已经完成。 默认超时为10.0秒,但可以通过调用该函数 setAsyncSpecTimeout(NSTimeInterval timeout) 来更改。
  • (before|after)(Each/All) 也接受 ^(DoneCallback done)
  • 如果您希望编写SPEC_BEGINSPEC_END而不是SpecBeginSpecEnd,请在导入 Specta 之前 #define SPT_CEDAR_SYNTAX
  • 在您的 describecontext exampleitspecify 之前添加 f 以便将重点放在示例或组上。当规格集中时,所有未集中的规格将被跳过。
  • 要使用原始 XCTest 报告器,请在测试方案中将名为 SPECTA_REPORTER_CLASS 的环境变量设置为 SPTXCTestReporter
  • 使用值 1 设置环境变量 SPECTA_SHUFFLE 以启用测试改组。
  • 设置环境变量 SPECTA_SEED 以指定用于测试改组的随机种子。

安装

使用 CocoaPods, Carthage or Set up manually

CocoaPods

  1. 将 Specta 添加到项目的 Podfile:
target :MyApp do
# your app dependencies
  target :MyAppTests do
    inherit! :search_paths
    pod 'Specta', '~> 1.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
  1. 在项目目录中运行 pod install

Carthage

  1. 将 Specta 添加到项目的 Cartfile.private 中
    github "specta/specta" ~> 1.0
        
  2. 在项目目录中运行 carthage update
  3. 将适合您平台的 Specta.framework (位于 Carthage/Build/) 拖到应用程序的Xcode 项目中,并将其添加到测试目标中。
  4. 如果要为 iOS 构建,则必须添加新的“运行脚本阶段”以复制框架。这些说明可以可以在 Carthage 入门说明 中找到。

手动设置

  1. 从 Github 克隆。
  2. 在项目根目录中运行 rake 来构建。
  3. 添加“Cocoa/Cocoa Touch Unit Testing Bundle”目标(如果您还没有)。
  4. 将 Products 文件夹中的所有头文件复制并添加到 Xcode 项目中的 Test 目标。
  5. 对于 OS X 项目,将 Products/osx 文件夹中的 Specta.framework 复制并添加到 Xcode 项目中的测试目标。 对于 iOS 项目,将 Products/ios 文件夹中的Specta.framework 复制并添加到 Xcode 项目中的测试目标。 如果您希望将其添加为项目的静态库,也可以使用 libSpecta.a。 (iOS 7 及以下版本需要此功能)
  6. 将 -ObjC 和 -all_load 添加到 Xcode 项目中测试目标的“Other Linker Flags”构建设置中。
  7. 如果遇到与 _llvm_ * 符号的链接问题,请确保目标的"Generate Test Coverage Files"(“生成测试覆盖率文件”)和"Instrument Program Flow"(“仪器程序流”)构建设置设置为“是”。

许可

版权所有 (c) 2012-2016 Specta Team。本软件是 MIT License 授权下使用的。

(First version: vz 2019/07/30)

主要指標

概覽
名稱與所有者specta/specta
主編程語言Objective-C
編程語言Ruby (語言數: 2)
平台Mac
許可證MIT License
所有者活动
創建於2012-01-06 08:15:03
推送於2022-02-27 13:22:51
最后一次提交
發布數28
最新版本名稱v2.0.0 (發布於 )
第一版名稱v0.1.0 (發布於 2012-01-09 15:54:12)
用户参与
星數2.3k
關注者數63
派生數214
提交數315
已啟用問題?
問題數124
打開的問題數16
拉請求數77
打開的拉請求數2
關閉的拉請求數28
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

Specta Build Status Coverage Status

A light-weight TDD / BDD framework for Objective-C.

FEATURES

  • An Objective-C RSpec-like BDD DSL
  • Quick and easy set up
  • Built on top of XCTest
  • Excellent Xcode integration

SCREENSHOT

EXAMPLE

#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.
  });
});

SpecEnd
  • beforeEach and afterEach are also aliased as before and after respectively.
  • describe is also aliased as context.
  • it is also aliased as example and specify.
  • itShouldBehaveLike is also aliased as itBehavesLike.
  • Use pending or prepend x to describe, context, example, it, and specify to 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 function setAsyncSpecTimeout(NSTimeInterval timeout).
  • (before, after)(Each/All) also accept ^(DoneCallback done)s.
  • Do #define SPT_CEDAR_SYNTAX before importing Specta if you prefer to write SPEC_BEGIN and SPEC_END instead of SpecBegin and SpecEnd.
  • Prepend f to your describe, context, example, it, and specify to 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_CLASS to SPTXCTestReporter in your test scheme.
  • Set an environment variable SPECTA_SHUFFLE with value 1 to enable test shuffling.
  • Set an environment variable SPECTA_SEED to 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.

STATUS

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.

RUNNING SPECTA'S TESTS IN COMMAND LINE

  • Run rake test in the cloned folder.

CONTRIBUTION GUIDELINES

  • 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.

Installation

Use CocoaPods, Carthage or Set up manually

CocoaPods

  1. Add Specta to your project's Podfile:
target :MyApp do
# your app dependencies

  target :MyAppTests do
    inherit! :search_paths

    pod 'Specta', '~> 1.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
  1. Run pod install in your project directory.

Carthage

  1. Add Specta to your project's Cartfile.private

    github "specta/specta" ~> 1.0
    
  2. Run carthage update in your project directory

  3. Drag the appropriate Specta.framework for your platform (located in Carthage/Build/) into your application’s Xcode project, and add it to your test target(s).

  4. If you are building for iOS, a new Run Script Phase must be added to copy the framework. The instructions can be found on Carthage's getting started instructions

SETTING UP MANUALLY

  1. Clone from Github.
  2. Run rake in project root to build.
  3. Add a "Cocoa/Cocoa Touch Unit Testing Bundle" target if you don't already have one.
  4. Copy and add all header files in Products folder to the Test target in your Xcode project.
  5. For OS X projects, copy and add Specta.framework in Products/osx folder to the test target in your Xcode project.
    For iOS projects, copy and add Specta.framework in Products/ios folder to the test target in your Xcode project.
    You can alternatively use libSpecta.a, if you prefer to add it as a static library for your project. (iOS 7 and below require this)
  6. Add -ObjC and -all_load to the "Other Linker Flags" build setting for the test target in your Xcode project.
  7. If you encounter linking issues with _llvm_* symbols, ensure your target's "Generate Test Coverage Files" and "Instrument Program Flow" build settings are set to Yes.

LICENSE

Copyright (c) 2012-2016 Specta Team. This software is licensed under the MIT License.