由 testInvocations 动态生成的 XCTest 方法是否适用于 xcodebuild 的 -only-testing?

Does XCTest methods generated dynamically by testInvocations work with xcodebuild's -only-testing?

提问人:Bartek Pacia 提问时间:11/16/2023 更新时间:11/16/2023 访问量:13

问:

我有一个应用程序,它需要在其子类中动态生成测试方法。我用于在运行时动态生成具有以下(虚拟)名称的测试方法:和 .下面是一个简化的、可以粘贴到 Xcode 中的代码。XCTestCase+(NSArray<NSInvocation*>*)testInvocationsexample_testpermissions_location_testpermissions_many_test

@import XCTest;
@import ObjectiveC.runtime;

@interface ParametrizedTests : XCTestCase
@end

@implementation ParametrizedTests
+ (NSArray<NSInvocation *> *)testInvocations {
  NSLog(@"testInvocations() called");

  /* Prepare dummy input */
  __block NSMutableArray<NSString *> *dartTestFiles = [[NSMutableArray alloc] init];
  [dartTestFiles addObject:@"example_test"];
  [dartTestFiles addObject:@"permissions_location_test"];
  [dartTestFiles addObject:@"permissions_many_test"];

  NSMutableArray<NSInvocation *> *invocations = [[NSMutableArray alloc] init];

  NSLog(@"Before the loop, %lu elements in the array", (unsigned long)dartTestFiles.count);

  for (int i = 0; i < dartTestFiles.count; i++) {
    /* Step 1 */

    NSString *name = dartTestFiles[i];

    void (^anonymousFunc)(ParametrizedTests *) = ^(ParametrizedTests *instance) {
      NSLog(@"anonymousFunc called!");
    };

    IMP implementation = imp_implementationWithBlock(anonymousFunc);
    NSString *selectorStr = [NSString stringWithFormat:@"test_%@", name];
    SEL selector = NSSelectorFromString(selectorStr);
    class_addMethod(self, selector, implementation, "v@:");

    /* Step 2 */

    NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.selector = selector;

    NSLog(@"RunnerUITests.testInvocations(): selectorStr = %@", selectorStr);

    [invocations addObject:invocation];
  }

  NSLog(@"After the loop");

  return invocations;
}

@end

我可以使用以下命令一次运行所有这些测试:

xcodebuild test \
  -scheme Landmarks \
  -destination 'platform=iOS Simulator,name=iPhone 15'

摘自上述命令的 stdout:

Test Suite 'Selected tests' passed at 2023-11-16 13:44:59.148.
     Executed 3 tests, with 0 failures (0 unexpected) in 0.246 (0.248) seconds

问题

我现在面临的问题是,我不能只选择一个测试来使用 ' 标志运行。例如:xcodebuild-only-testing

xcodebuild test \
  -scheme Landmarks \
  -destination 'platform=iOS Simulator,name=iPhone 15' \
  -only-testing 'LandmarksUITests/ParametrizedTests/example_test'

不起作用 - 不执行任何测试:

Test Suite 'ParametrizedTests' passed at 2023-11-16 13:45:58.472.
     Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.000) seconds

我也试过做:

xcodebuild test \
  -scheme Landmarks \
  -destination 'platform=iOS Simulator,name=iPhone 15' \
  -only-testing 'LandmarksUITests/ParametrizedTests/testInvocations'

但结果是一样的。

所以问题是:我怎样才能选择带有-only-testing选项的测试子集(在运行时使用testInvocations动态生成)?甚至有可能吗?

ios objective-c xctest xcuitest

评论


答: 暂无答案