簡體   English   中英

在Xcode 6中使用XCTest進行訂購單元測試

[英]Ordering unit test using XCTest in Xcode 6

我想知道是否有任何方法告訴Xcode以指定的順序運行單元測試。 我的意思是不是在同一個XCTestCase類文件中,而是在所有類文件之間。

在此輸入圖像描述

例如,我想在運行SitchozrSDKMessageTest之前運行SitchozrSDKSessionTest。

我在堆棧或Apple文檔上查看了幾個線程,但我沒有找到有用的東西。

謝謝你的幫助。

它按字母順序排序(類和方法)。 因此,當您需要最后運行的某些測試時,只需更改Class或Method的名稱(對於(討厭的)示例,通過前綴'z_')。

所以...你有類名:

MyAppTest1
  -testMethodA
  -testMethodB
MyAppTest2
  -testMethodC
  -testMethodD

他們按此順序運行。 如果你需要運行MyAppTest1作為第二個,只需重命名,以便它的名稱按字母順序排在MyAppTest2(z_MyAppTest1)之后,它將運行(方法相同):

MyAppTest2
  -a_testMethodD
  -testMethodC
z_MyAppTest1
  -testMethodA
  -testMethodB

另請以命名為例:)

確實,當前(從Xcode 7開始),XCTestCase方法按字母順序運行,因此您可以通過巧妙地命名它們來強制命令。 但是,這是一個實現細節,似乎可能會改變。

一個(希望)不那么脆弱的方法是覆蓋+[XCTestCase testInvocations]並按照您希望測試運行的順序返回您自己的NSInvocation對象。

就像是:

+ (NSArray *)testInvocations
{
    NSArray *selectorStrings = @[@"testFirstThing",
                                 @"testSecondThing",
                                 @"testAnotherThing",
                                 @"testLastThing"];

    NSMutableArray *result = [NSMutableArray array];
    for (NSString *selectorString in selectorStrings) {
        SEL selector = NSSelectorFromString(selectorString);
        NSMethodSignature *methodSignature = [self instanceMethodSignatureForSelector:selector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
        invocation.selector = selector;
        [result addObject:invocation];
    }
    return result;
}

當然,這里的缺點是你必須手動添加每個測試方法,而不是自動拾取它們。 有幾種方法可以改善這種情況。 如果你只需要在你的+testInvocations覆蓋中訂購一些測試方法,而不是所有測試方法,你可以調用super,過濾掉你手動訂購的那些方法,然后將其余部分添加到最后你返回的數組。 如果您需要訂購所有測試方法,您仍然可以獲得調用super的結果,並驗證手動創建的有序結果是否涵蓋了所有自動拾取的方法。 如果沒有,您可以斷言,如果您忘記添加任何方法,則會導致失敗。

我不討論編寫必須以特定順序運行的測試是否“正確”的討論。 我認為很少有這種情況有意義,但其他人可能不同意。

按函數名稱字母順序排序,無論你如何在代碼中訂購它。 例如:

-(void)testCFun(){};
-(void)testB2Fun(){};
-(void)testB1Fun(){};

實際的執行順序是:

  1. testB1Fun調用
  2. testB2Fun叫
  3. testCFun調用

除了安德魯 - 馬德森的回答:
我創建了一個'智能' testInvocations類方法,該方法搜索以“test”開頭的選擇器並按字母順序對它們進行排序。
因此,您不必單獨維護選擇器名稱的NSArray

#import <objc/runtime.h>

+ (NSArray <NSInvocation *> *)testInvocations
{
    // Get the selectors of this class
    unsigned int mc = 0;
    Method *mlist = class_copyMethodList(self.class, &mc);
    NSMutableArray *selectorNames = [NSMutableArray array];
    for (int i = 0; i < mc; i++) {
        NSString *name = [NSString stringWithFormat:@"%s", sel_getName(method_getName(mlist[i]))];
        if (name.length > 4
            && [[name substringToIndex:4] isEqualToString:@"test"]) {
            [selectorNames addObject:name];
        }
    }

    // Sort them alphabetically
    [selectorNames sortUsingComparator:^NSComparisonResult(NSString * _Nonnull sel1, NSString * _Nonnull sel2) {
        return [sel1 compare:sel2];
    }];

    // Build the NSArray with NSInvocations
    NSMutableArray *result = [NSMutableArray array];
    for (NSString *selectorString in selectorNames) {
        SEL selector = NSSelectorFromString(selectorString);
        NSMethodSignature *methodSignature = [self instanceMethodSignatureForSelector:selector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
        invocation.selector = selector;
        [result addObject:invocation];
    }
    return result;
}

腳注:認為單元測試依賴於彼此是不好的做法

例如,您可以像這樣重命名項目中的測試文件:

SitchozrSDKSessionTest -> t001_SitchozrSDKSessionTest
SitchozrSDKMessageTest -> t002_SitchozrSDKMessageTest

Xcode使用字母順序處理文件。

從Xcode 7開始,XCTestCase子類按字母順序排列。 如果要確保測試方法本身也按字母順序運行,則必須覆蓋testInvocations類方法並返回按選擇器排序的調用。

@interface MyTestCase : XCTestCase
@end

@implementation MyTestCase

+ (NSArray<NSInvocation *> *) testInvocations
{
    return [[super testInvocations] sortedArrayUsingComparator:^NSComparisonResult(NSInvocation *invocation1, NSInvocation *invocation2) {
        return [NSStringFromSelector(invocation1.selector) compare:NSStringFromSelector(invocation2.selector)];
    }];
}

- (void) test_01
{
}

- (void) test_02
{
}

@end

OP未提及CI是否可用或命令行答案是否足夠。 在那些情況下,我很喜歡使用xctool [1]。 它具有將測試運行到一個測試方法的語法:

path/to/xctool.sh \
  -workspace YourWorkspace.xcworkspace \
  -scheme YourScheme \
  test -only SomeTestTarget:SomeTestClass/testSomeMethod

我會這樣做,以確保我們測試我們認為最容易的項目。

1. [] facebook / xctool:Apple xcodebuild的替代品,可以更輕松地構建和測試iOS或OSX應用程序。 ; ; https://github.com/facebook/xctool

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM