Test discovery on Linux

I've been slowly working on test discovery for Linux and I think the implementation is in a reasonable state now. I've tested it on some OSS Swift packages but it would be great if others can try it out and report any issues they run into. It can be enabled by passing --enable-test-discovery flag to the swift test invocation.

The toolchain is available here.

Known issues:

  • Running tests in release mode requires passing --enable-index-store flag.

Would would be really useful is being able to do a diff between user defined linuxmain files and what is auto generated and in this way I have more confidence that I can just delete all the manual files.

You can diff the output of swift test --list-tests and swift test --enable-test-discovery --list-tests.

You can also see the generated test list in .build/x86_64-unknown-linux/debug/testlist.derived if you want to do a manual inspection.


This is really amazing Ankit, thanks a lot for publishing the toolchain too.

I just tried on some projects I have laying around and found no issues in discovering tests in any of them, it correctly discovered all of our tests :heart:

I have discovered one difference though; which we actually do not rely on, but perhaps someone could / does, so might as well mention the case right away, even if we'd e.g. ship a first version without supporting that (which would already be tremendously useful, as I don't know if people actually do this base class "trick"):


  • class CommonTests: XCTestCase { func test_common() {} }
  • final class SpecificOneTests: CommonTests {}

On mac this discovers the SpecificOneTests.test_common as well as the one in the base class, the current --enable-test-discovery does not discover the one in the SpecificOneTests.

Having that said, I'm not sure how critical this is, since I've not seen it used, but I could be totally mistaken and people could expect it to work. Even the existing PRed discovery would already be amazing and I'd love to be able to use it in normal swift builds :slight_smile:

1 Like

I created a docker image on Docker Hub as norionomura/swift:pr-25685 that supports --enable-test-discovery.


Ah, yeah. This is a little complex to implement in the current approach but it should be technically possible. I don't think this is a common enough scenario though and it is easy to workaround (by just explicitly writing a new test method in the subclass and then calling the method in the super class).

1 Like

Yeah, agreed that it's not a show stopper; wanted to make sure we're aware of it (might want to document it).

All in all though this is awesome, can't wait :blush:

1 Like

thank you for putting this together @Aciid, this is fantastic for server developers


I've just been trying out the new --enable-test-discovery flag on the latest Swift 5.1 docker image and found it's great for tests, so thanks for the great work :+1:t2:

Unfortunately I've found that just running swift build still requires the LinuxMain.swift file to exist, even though it's not needed any more. I'm not sure why just doing a plain build without the tests requires the LinuxMain.swift file. Is there a reason for this and are there any plans to change it in future?


Just hit this issue when building an app inside Docker that's had the LinuxMain and all the allTests stripped out as it's no longer needed. A completely empty LinuxMain.swift fixes the issue but as mentioned it seems weird to require it for the build to pass (I'm assuming it's because the build command also attempts to build the Test target as well). It also isn't required when building on macOS. Another fix is to build it with the --enable-test-discovery flag as well.

can you check if this is an issue with the trunk snapshot?

Terms of Service

Privacy Policy

Cookie Policy