Swift Package Unit Test "Button" in Xcode not working

Posted this in Apple's internal forums, no response, so hoping someone here can shed light on whether this is a known bug (it would have been there for many years!) or operator error.

I have a Swift Package, all Swift. I have a unit text file (XCTestCase). If I select "Test" using the button in the Window Title Bar, it works fine (meaning, all tests are run, in all files).

If I select the diamond shaped button in the "Test Navigator" pane, either the File Name itself or a test within the file, Xcode acts like it ran some test, but there is no test console output - just this:

2. Test Suite 'SAX-CSV-ParserTests.xctest' started at 2020-02-20 16:52:03.829
3. Test Suite 'SAX-CSV-ParserTests.xctest' passed at 2020-02-20 16:52:03.829.  
4. Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.000) seconds  
5. Test Suite 'Selected tests' passed at 2020-02-20 16:52:03.829.  
6. Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.001) seconds  
7. Program ended with exit code: 0``` 

Is this a known problem with Swift Packages? Tried this with Xcode 11.3.1 and 11.4-beta2 - no difference. Is there a workaround?

I have never seen this happen. Two questions:

  1. What are some names of tests that get skipped?
  2. Does the equivalent swift test --filter TestModule.TestClass/testMethod work?
1 Like

Hmmm - I never go an indication you replied to this.

I had someone else verify the issue - and you can do it easily. Create a new package with Xcode 11.3 or 11.4. There is a single test created that just verifies a string. Add a second test that fails - test against a different string. Now click on the diamonds. Xcode will act as if its doing the test, but it doesn't! The new bad test will pass! If you from the top left button run ALL the tests, the first passes, the second fails. But the single diamond button to run one test does nothing.

In answer to part 2, the command line works - the result as you would expect (in the above case, one success, one failure)

I was only able to figure it out by tracking down your particular package.

The deciding factor is the target name: SAX-CSV-Parser-Tests. That is not a valid Swift module name. The package manager sanitizes it for you, so the module is actually called SAX_CSV_Parser_Tests. (You may have noticed that you have a target named SAX-CSV-Parser, but in the test sources it is imported with import SAX_CSV_Parser.)

When you use SwiftPM’s command line filter, it only works if you use the real module name. The target name does not get matched:

$ swift test --filter SAX-CSV-Parser-Tests.XCTest_Delegate/test000_ASCII
[9/9] Linking SAX-CSV-ParserPackageTests
'--filter' predicate did not match any test case

$ swift test --filter SAX_CSV_Parser_Tests.XCTest_Delegate/test000_ASCII
Test Suite 'Selected tests' started at 2020-03-01 15:10:55.529
Test Suite 'SAX-CSV-ParserPackageTests.xctest' started at 2020-03-01 15:10:55.529
Test Suite 'XCTest_Delegate' started at 2020-03-01 15:10:55.529
Test Case '-[SAX_CSV_Parser_Tests.XCTest_Delegate test000_ASCII]' started.
Test Case '-[SAX_CSV_Parser_Tests.XCTest_Delegate test000_ASCII]' passed (0.150 seconds).
Test Suite 'XCTest_Delegate' passed at 2020-03-01 15:10:55.679.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.150 (0.150) seconds
Test Suite 'SAX-CSV-ParserPackageTests.xctest' passed at 2020-03-01 15:10:55.679.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.150 (0.150) seconds
Test Suite 'Selected tests' passed at 2020-03-01 15:10:55.679.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.150 (0.151) seconds

Presumably Xcode is filtering the module/class/method combination according to the user‐facing strings displayed in the UI and not finding any matches among the real compiled symbols.

The immediate workaround is to rename the target with a name that can also be used as‐is for the module. You can report the bug to the Xcode team with the Feedback Assitant.

3 Likes

Jeremy, just returned from a trip (yeah!) and saw your response. I had actually done a second test, where I created a new project, then added a new test that I knew would fail, but it passed. Grrr - I probably put a dash in the name of that one too!

Filed a feedback on this too: FB7632455 , with two example projects.

Thank you ever so much for your deep dive into this issue!!!