7 C
Thursday, November 16, 2023

ios – Does XCTest strategies generated dynamically by testInvocations work with xcodebuild’s -only-testing?

I’ve an app which must dynamically generate check strategies in its XCTestCase subclass. I am utilizing +(NSArray<NSInvocation*>*)testInvocations to dynamically generate check strategies at runtime with the next (dummy) names: example_test, permissions_location_test and permissions_many_test. Under is a simplified, ready-to-be-pasted-into-Xcode code.

@import XCTest;
@import ObjectiveC.runtime;

@interface ParametrizedTests : XCTestCase

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

  /* Put together dummy enter */
  __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(@"Earlier than the loop, %lu parts within the array", (unsigned lengthy)dartTestFiles.depend);

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

    NSString *title = dartTestFiles[i];

    void (^anonymousFunc)(ParametrizedTests *) = ^(ParametrizedTests *occasion) {
      NSLog(@"anonymousFunc referred to as!");

    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;


I can run all these assessments without delay utilizing:

xcodebuild check 
  -scheme Landmarks 
  -destination 'platform=iOS Simulator,title=iPhone 15'

Excerpt from above command’s stdout:

Check Suite 'Chosen assessments' handed at 2023-11-16 13:44:59.148.
     Executed 3 assessments, with 0 failures (0 sudden) in 0.246 (0.248) seconds


The issue I am going through now could be that I can not choose only one check to run utilizing xcodebuild‘s -only-testing flag. For instance:

xcodebuild check 
  -scheme Landmarks 
  -destination 'platform=iOS Simulator,title=iPhone 15' 
  -only-testing 'LandmarksUITests/ParametrizedTests/example_test'

doesn’t work – no assessments are executed:

Check Suite 'ParametrizedTests' handed at 2023-11-16 13:45:58.472.
     Executed 0 assessments, with 0 failures (0 sudden) in 0.000 (0.000) seconds

I additionally tried doing:

xcodebuild check 
  -scheme Landmarks 
  -destination 'platform=iOS Simulator,title=iPhone 15' 
  -only-testing 'LandmarksUITests/ParametrizedTests/testInvocations'

however the end result is similar.

So the query is: how can I choose a subset of assessments (that have been generated dynamically at runtime utilizing testInvocations) with the -only-testing possibility?. Is is even attainable?

Latest news
Related news


Please enter your comment!
Please enter your name here