[swift-build-dev] Draft proposal: "Test Executables"

George King george.w.king at gmail.com
Tue Nov 15 12:16:16 CST 2016


Daniel:

It looks like your last reply was referencing the original draft I sent in July, not the 2nd draft I sent 11/10. Pasted below is a 3rd draft, removing the bit about @testable.

Regarding your second comment, I had previously removed the stuff about outputting a manifest. I agree with both of your responses.

Ankit:

> This proposal sounds similar to allowing regular modules (lib+exec) inside Tests/ and IMO that is better than just allowing executables and is more general. Right now we error out when some directory doesn't have Tests prefix in Tests/ folder, We can just allow that and use same rule of main.swift to distinguish between lib and exec.


Expanding the change to lib+exec seems reasonable. However, my understanding is that the motivation for supporting lib is a bit different. At the moment my proposal is almost entirely focused on motivation. If the change gets made, I'm happy to drop the proposal, but I just want to be clear about the use case.

Thanks,
George


Test Executables

Proposal: SE-NNNN <applewebdata://E6A571B4-6571-4D9B-B3EB-624D50AAC773/NNNN-filename.md>
Author: George King <https://github.com/gwk>
Status: Awaiting review / DRAFT 3
Review manager: TBD
Introduction

The package manager currently supports testing via XCTest, but does not provide support for other testing methodologies. In particular, facilities for building test executables (that is, executables meant strictly for testing and not as an end product) in the Swift Package Manager would make command line testing of Swift packages easier and more robust. We propose to add to the package manager the ability to specify and build test-only executable targets that are distinct from regular executables.

Motivation

Integration testing <https://en.wikipedia.org/wiki/Integration_testing> is, broadly speaking, the practice of testing multiple software components such as functions and classes (which may be "unit tested" individually) in aggregate. Integration tests can reveal bugs arising from interactions between components, can validate the intended usage patterns for individual components, and serve as references for that intended usage.

XCTest offers facilities <https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/03-testing_basics.html#//apple_ref/doc/uid/TP40014132-CH3-SW8> for unit testing, performance testing, and GUI testing, but not for testing the basic IO behavior of a process. To be clear, this proposal does not address XCTest at all; while adding whole process, IO oriented APIs to XCTest sounds desirable, it is a larger project and out of scope for this proposal.

Instead, this proposal simply asks for the ability to build executables specifically for testing, rather than for public usage. The intended use case is execution via an external test harness, or from within an XCTest case via NSTask/Process.

Consider the existing features of swiftpm as two orthogonal axes:

Build, Test
Library, Executable
Of the four combinations, only "Test Executable" is missing.

Currently, test executables can simply be placed in the Sources, where they are compiled as regular executable targets. Distinguishing them as test executables would be helpful for several reasons:

Test executables would only be built by swift test, not swift build.
Putting all test code in the Tests directory will prevent test executables from being exposed to library consumers, and clarifies developer intent.
For simple projects that produce a single library module, adding a test executable will no longer necessitate moving the library code from Sources/ to Sources/[Module] to allow for the second target.
Proposed solution

swift test should distinguish between unit test directories and executable directories, just as swift build distinguishes between libary and executable directories.

The swift test command should, upon encountering a test executable directory, build the executable.

If the test executable build fails, swift test should report a test failure and return a nonzero exit code. An external test harness can then run the test executables as it sees fit. Note that for an XCTest case to invoke the test executable, test executables would need to be built prior to running test cases.

Future improvements could include invoking an external test command specified in Package.swift upon successful build.

Detailed design

TODO.

Impact on existing code

None; swift test currently appears to ignore subdirectories in Tests that do not contain XCTest-based unit tests.

Alternatives considered

No action

See the list of problems with treating test executables as regular targets in the "Motivation" section above.

Complete integration testing with XCTest

This would be a much larger undertaking, and it seems likely that such a solution would necessitate the essentials of this proposal anyway.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-build-dev/attachments/20161115/1fa7d59e/attachment.html>


More information about the swift-build-dev mailing list