0
votes

I added a Swift class to an ObjC project and wanted to write some unit tests for it. But when I go to build, XCode complains that it can't find the interface declaration for XCTestCase, even though the @import of XCTest is just above. What am I doing wrong?

Here is the relevant part of the generated -FooTests-Swift.h file, with annotations:

#if defined(__has_feature) && __has_feature(modules)
@import ObjectiveC;
@import XCTest;                <<<< Note the import
#endif

#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"
#pragma clang diagnostic ignored "-Wduplicate-method-arg"

SWIFT_CLASS("_TtC12play611Tests14WeightedSumIn2")
@interface WeightedSumIn2 : NSObject   <<<< Error here: Cannot find
                                       <<<< interface declaration for 'XCTestCase'
- (SWIFT_NULLABILITY(nonnull) instancetype)initWithInitValue:(double)initValue OBJC_DESIGNATED_INITIALIZER;
- (double)push:(double)e;
@end

@class NSInvocation;

SWIFT_CLASS("_TtC12play611Tests18WeightedSumIn2Test")
@interface WeightedSumIn2Test : XCTestCase
- (void)test1;
- (SWIFT_NULLABILITY(null_unspecified) instancetype)initWithInvocation:(NSInvocation * __null_unspecified)invocation OBJC_DESIGNATED_INITIALIZER;
- (SWIFT_NULLABILITY(null_unspecified) instancetype)initWithSelector:(SEL __null_unspecified)selector OBJC_DESIGNATED_INITIALIZER;
- (SWIFT_NULLABILITY(nonnull) instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

Here is the source for the test case:

import UIKit
import XCTest

class WeightedSumIn2Test : XCTestCase {
    func test1() {...}
}

and for the class:

import UIKit

public class WeightedSumIn2 : NSObject {
    init(initValue: Double = 0.0) {...}
    func push(e: Double) -> Double {...}
}

Both files have been added to the test target. Also, I can build/run ObjC unit tests, just not this Swift one.

Any idea what's wrong? If that import of XCTest is actually being done (how can I know whether that #if condition is true?), shouldn't the interface for XCTestCase be available?

1
Did you add the tests files to the target? Interesting elements in this post.Eric Aya
Good question, but yes, test files were added to the test target.AmigoNico
have you managed to find the answer?Mr. L
Hmmm. I forgot I asked here, and now I can't remember how I solved the problem. Perhaps I should delete this question...AmigoNico

1 Answers

-1
votes

One thing that could be the issue is this:

Does the swift file for WeightedSumIn2 declare Target Membership to both you're test bundle and your main bundle? If not, select the swift file and in the right menu, under Target Membership, select your test bundle as well (it should be a checkmark).