是否有用于Objective-C的非基于Xcode的命令行单元测试工具? [英] Is there any non-Xcode-based command line unit testing tool for Objective-C?

查看:58
本文介绍了是否有用于Objective-C的非基于Xcode的命令行单元测试工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帖子在没有GUI的情况下编译Objective-C 表示:

要在OSX上编译Objective-C,必须轻松获得XCode,可从Application Store免费获得XCode.获得XCode将确保您获得必需的框架(标头),例如Foundation,Cocoa等.但是,这将不会为您提供从命令行编译Object-C所必需的命令行工具.打开XCode,转到首选项">下载">组件",然后安装命令行工具.这将安装gcc,clang,make等.

我正在寻找一个非基于Xcode的工具来解决这个问题,我刚刚打开:

I am looking for a non-Xcode-based tool to address this my question, I've just opened: Is it possible to make an Objective-C project to be tested on Travis?

此工具应满足以下要求:

This tool should meet the following requirements:

  • It should be not related to Xcode in any way.
  • The following level of simplicity is pretty enough: just some int main {} code collecting all test-cases files nearby and running test assertions on the code I want to test (like ST- in SetTestingKit or GH- in GHUnit)
  • I don't need UI, GUI, Xcode, simulator.
  • It will be great if it could work both on Mac and Ubuntu (yeah, Travis), possibly using GNUstep like the quoted post describes.

基于接受的答案的更新:

基于Malte Tancred在接受的答案中所说的内容,以下简单设置似乎足以满足我当前的需求:

The following simple setup, based on what Malte Tancred have said in the accepted answer, seems pretty enough for my current needs:

三个文件,都在我项目的tests目录中:

Three files, all in the tests directory of my project:

octest.m

#import <Foundation/Foundation.h>
#import <SenTestingKit/SenTestingKit.h>

int main() {
    @autoreleasepool {
        SenSelfTestMain();
    }

    return 0;
}

Makefile

CC=clang # or gcc

# Trick to get current dir: https://stackoverflow.com/questions/322936/common-gnu-makefile-directory-path
TESTS_DIR:= $(dir $(lastword $(MAKEFILE_LIST)))
PROJECT_DIR:= $(TESTS_DIR)/..

FRAMEWORKS_PATH:= -F/Applications/Xcode.app/Contents/Developer/Library/Frameworks
FRAMEWORKS:= -framework Foundation -framework SenTestingKit
LIBRARIES:= -lobjc
INCLUDE_PATHS:= -I$(PROJECT_DIR)/Projectfiles\
                -I$(TESTS_DIR)/TestHelpers

SOURCE_FILES = $(wildcard $(PROJECT_DIR)/Projectfiles/*.m)

SOURCE_TEST_SUITE = $(wildcard $(TESTS_DIR)/Tests/*.m)

SOURCE_TESTS = $(TESTS_DIR)/TestHelpers/TestHelpers.m\
                 octest.m


CFLAGS=-Wall -Werror -fobjc-arc -g -v $(SOURCE_FILES) $(SOURCE_TEST_SUITE) $(SOURCE_TESTS)
LDFLAGS=$(LIBRARIES) $(FRAMEWORKS)
OUT=-o octest

all:
    $(CC) $(FRAMEWORKS_PATH) $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(OUT)

运行测试

#!/bin/bash
export DYLD_FRAMEWORK_PATH=/Applications/Xcode.app/Contents/Developer/Library/Frameworks
make
./octest

更新为捕获退出代码:

在我问了这个问题几天后,特拉维斯宣布了对Objective-C的支持:

A few days after I had asked this question, Travis announced Objective-C support:

http://about.travis-ci. org/blog/introducing-mac-ios-rubymotion-testing/

尽管他们建议使用默认脚本来进行构建,但我还是决定采用此处描述的方法,并且仍然使用octest代替Travis使用的xcodebuild方法.

Though there are default scripts they suggest to use to make builds, I have decided to take the approach described here and still use octest instead of approach with xcodebuild that Travis uses.

默认情况下,travis设置依赖于Justin Spahr-Summers编写的构建脚本: https://github.com/jspahrsummers/objc-build-scripts :

By default travis setup relies on the build scripts written by Justin Spahr-Summers: https://github.com/jspahrsummers/objc-build-scripts:

他们使用 awk 从xcodebuild的输出中捕获退出代码,因为即使整个测试套件都失败了,退出代码始终存在0退出代码!

They use awk to capture exit code from xcodebuild's output, since it always exists with 0 exit code, even if the whole test suite has failing!

OCTest的行为方式相同-它始终以0代码存在,这是我使用Travis awk 脚本的简化版本来满足我如上所述的方式构建它的需要:

OCTest behaves the same way - it always exists with 0 code, and here is how I've used simplified version of Travis awk script for my needs of building it the way I decribed above:

octest.awk

# Exit statuses:
#
# 0 - No errors found.
# 1 - Build or test failure. Errors will be logged automatically.

BEGIN {
    status = 0;
}

{
    print;
    fflush(stdout);
}

/[0-9]+: (error|warning):/ {
    errors = errors $0 "\n";
}

/with [1-9]+ failures?/ {
    status = 1;
}

END {
    if (length(errors) > 0) {
        print "\n*** All errors:\n" errors;
    }

    fflush(stdout);
    exit status;
}

运行测试

#!/bin/bash

export DYLD_FRAMEWORK_PATH=/Applications/Xcode.app/Contents/Developer/Library/Frameworks

make

runtests ()
{
  ./octest 2>&1 | awk -f "octest.awk"

  local awkstatus=$?

  if [ "$awkstatus" -eq "1" ]
  then
    echo "Test suite failed"
    return $awkstatus
  else
    echo "Test suite passed"
    return 0
  fi
}

echo "*** Building..."

runtests || exit $?

推荐答案

您的主要问题的答案是肯定的: 有用于Objective-C的命令行测试工具 不依赖于Xcode.

The answer to your main question is yes: there are command line testing tools for Objective-C that does not depend on Xcode.

例如, 您可以在不使用Xcode的情况下使用OCUnit/SenTestingKit. 所有你需要做的 是将您的编译器/链接器指向框架. 考虑以下文件octest.m:

For example, you can use OCUnit/SenTestingKit without using Xcode. All you have to do is to point your compiler/linker to the framework. Consider the follwing file, octest.m:

#import <Foundation/Foundation.h>
#import <SenTestingKit/SenTestingKit.h>

int main() {
  @autoreleasepool {
    SenSelfTestMain();
  }
  return 0;
}

@interface MyTest : SenTestCase
@end

@implementation MyTest
- (void)testSomething {
  STAssertEquals(1, 2, @"fail");
}
@end

编译:

clang -o octest octest.m -F/Applications/Xcode.app/Contents/Developer/Library/Frameworks -framework Foundation -framework SenTestingKit

现在运行它

DYLD_FRAMEWORK_PATH=/Applications/Xcode.app/Contents/Developer/Library/Frameworks ./octest

您应该会看到类似的内容

and you should see something like

Test Suite '/tmp/octest(Tests)' started at 2013-04-04 12:34:49 +0000
Test Suite 'MyTest' started at 2013-04-04 12:34:49 +0000
Test Case '-[MyTest testSomething]' started.
octest.m:16: error: -[MyTest testSomething] : '1' should be equal to '2': fail
Test Case '-[MyTest testSomething]' failed (0.000 seconds).
Test Suite 'MyTest' finished at 2013-04-04 12:34:49 +0000.
Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) seconds
Test Suite '/tmp/octest(Tests)' finished at 2013-04-04 12:34:49 +0000.
Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.001) seconds
make: *** [default] Error 1

此示例确实取决于Xcode 它使用捆绑的SenTestingKit框架 但是构建和运行测试的一般过程 如上所述 不依赖于Xcode.

This example does depend on Xcode in that it uses the bundled SenTestingKit framework but the general process of building and running the tests as described above does not depend on Xcode.

现在, 使它在Linux系统上运行 您必须安装SenTestingKit(很可能是GNUstep), 但这些组件到位 构建和测试过程应该基本相同.

Now, to get this running on a linux system you'd have to install SenTestingKit (and most probably GNUstep), but with those components in place the build and test process should be essentially the same.

这篇关于是否有用于Objective-C的非基于Xcode的命令行单元测试工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆