Skip to content

Tests: Convert some tests BasicsTests to Swift Testing #8093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bkhouri
Copy link
Contributor

@bkhouri bkhouri commented Nov 1, 2024

Convert some BasicsTests from XCTest to Swift Testing to make use of parallelism and, in some cases, test parameterization

Motivation:

The XCTest run, by default, sequentially. Convert most of the WorkspaceTests from XCTests to Swift Testing to make use of parallelism, and in some cases, parameterized test cases.

Not all Test Suites in BasicsTests have been converted as some use helpers in swift-tools-core-support, which don't have a matching swift testing helper.

Modifications:

Convert XCTests to Swift Testing

Result:

Ran the equivalent of

for _ in $(seq 0 100); 
do
    swift test --enable-swift-testing --disable-xctest
done

and ensured there were no test-related failures.

Blocked by #8137
Requires swiftlang/swift#78300
Might conflict with #8210

public func isRunninginCI(file: StaticString = #filePath, line: UInt = #line) -> Bool {
return (ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil || ProcessInfo.processInfo.environment["CI"] != nil)
}

public func XCTSkipIfCI(file: StaticString = #filePath, line: UInt = #line) throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idiomatic way to do this with SwiftTesting would be a trait, something like this:

extension Trait where Self == Testing.ConditionTrait {
    static var skipInCI: Self {
        disabled("the test is being run on CI", ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil || ProcessInfo.processInfo.environment["CI"] != nil)
    }
}

@bkhouri bkhouri changed the title [DRAFT] Convert some tests BasicsTests to Swift Testing Convert some tests BasicsTests to Swift Testing Nov 7, 2024
@bkhouri bkhouri marked this pull request as ready for review November 7, 2024 13:13
@bkhouri
Copy link
Contributor Author

bkhouri commented Nov 7, 2024

Ready for review, though it may be blocked as not all pipeline builds run with Swift 6.0!

@bkhouri
Copy link
Contributor Author

bkhouri commented Nov 8, 2024

Ready for review, though it may be blocked as not all pipeline builds run with Swift 6.0!

Looks like the Selft-hosted macOS pipeline is now running the nightly build
https://ci.swift.org/job/swift-package-manager-with-xcode-self-hosted-PR-osx/4511/execution/node/49/log/

)
struct AsyncProcessTests {
@Test
func basics() throws {
do {
let process = AsyncProcess(args: "echo", "hello")
try process.launch()
let result = try process.waitUntilExit()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is presumably actually blocking the thread, which is dangerous in Swift Testing because tests run on the shared thread pool. Consider switching over to an asynchronous suspension.


// Check that there's no error if we try to create the directory again.
try! makeDirectories(dirPath)
#expect(throws: Never.self) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just throw the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I want to explicitly indicate a test intentions is to not have an exception raised. Otherwise, future test authors might be include to catch and handle the error!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably never catch thrown errors in tests unless the test is itself testing that an error should be thrown. In general, allow the error to propagate out so that the testing library (XCTest or Swift Testing) can gather optimal metadata for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, but how do we handle the case where we explicitly want to validate that an error did not occur?

@bkhouri bkhouri marked this pull request as ready for review December 19, 2024 01:25
@bkhouri bkhouri added the test suite improvements to SwiftPM test suite label Dec 20, 2024
@bkhouri
Copy link
Contributor Author

bkhouri commented Jan 15, 2025

@swift-ci please test

@bkhouri bkhouri force-pushed the t/main/convert_from_xctests_to_swift-testing branch from 9bb616d to 7eebb04 Compare January 15, 2025 16:59
@bkhouri
Copy link
Contributor Author

bkhouri commented Jan 15, 2025

@swift-ci please test

@bkhouri bkhouri force-pushed the t/main/convert_from_xctests_to_swift-testing branch from 7eebb04 to 4593699 Compare January 15, 2025 19:04
@bkhouri
Copy link
Contributor Author

bkhouri commented Jan 15, 2025

@swift-ci please test

bkhouri added a commit that referenced this pull request Jan 16, 2025
Add a canary swift testing tests to ensure we do not regress.

This test will be removed sometime after #8092, #8093 or #8100 are
merged
@bkhouri bkhouri force-pushed the t/main/convert_from_xctests_to_swift-testing branch from 4593699 to d252933 Compare January 17, 2025 06:09
@bkhouri
Copy link
Contributor Author

bkhouri commented Jan 17, 2025

@swift-ci please test

bkhouri added a commit to bkhouri/swift-package-manager that referenced this pull request Jan 31, 2025
Add a canary swift testing tests to ensure we do not regress.

This test will be removed sometime after swiftlang#8092, swiftlang#8093 or swiftlang#8100 are
merged

(cherry picked from commit 257e671)
bkhouri added a commit to bkhouri/swift-package-manager that referenced this pull request Feb 6, 2025
Add a canary swift testing tests to ensure we do not regress.

This test will be removed sometime after swiftlang#8092, swiftlang#8093 or swiftlang#8100 are
merged

(cherry picked from commit 257e671)
bkhouri added a commit to bkhouri/swift-package-manager that referenced this pull request Feb 6, 2025
Add a canary swift testing tests to ensure we do not regress.

This test will be removed sometime after swiftlang#8092, swiftlang#8093 or swiftlang#8100 are
merged

(cherry picked from commit 257e671)
bkhouri added a commit that referenced this pull request Feb 10, 2025
Add a canary swift testing tests to ensure we do not regress.

This test will be removed sometime after #8092, #8093 or #8100 are
merged

(cherry picked from commit 257e671)

Re-adds #8222, after it was reverted in #8266 
Depends on swiftlang/swift#79074
bripeticca pushed a commit to bripeticca/swift-package-manager that referenced this pull request Feb 28, 2025
Add a canary swift testing tests to ensure we do not regress.

This test will be removed sometime after swiftlang#8092, swiftlang#8093 or swiftlang#8100 are
merged
bripeticca pushed a commit to bripeticca/swift-package-manager that referenced this pull request Feb 28, 2025
Add a canary swift testing tests to ensure we do not regress.

This test will be removed sometime after swiftlang#8092, swiftlang#8093 or swiftlang#8100 are
merged

(cherry picked from commit 257e671)

Re-adds swiftlang#8222, after it was reverted in swiftlang#8266 
Depends on swiftlang/swift#79074
@bkhouri
Copy link
Contributor Author

bkhouri commented Mar 28, 2025

@swift-ci please test linux

Convert some BasicsTests from XCTest to Swift Testing to make use of
parallelism and, in some cases, test parameterization.

Not all Test Suites in BasicsTests have been converted as some use helpers
in swift-tools-core-support, which don't have a matching swift testing
helper.
@bkhouri bkhouri force-pushed the t/main/convert_from_xctests_to_swift-testing branch from d252933 to 7e995b7 Compare March 28, 2025 16:10
@bkhouri bkhouri changed the title Convert some tests BasicsTests to Swift Testing Tests: Convert some tests BasicsTests to Swift Testing Mar 28, 2025
@bkhouri
Copy link
Contributor Author

bkhouri commented Mar 28, 2025

@swift-ci please test

@bkhouri
Copy link
Contributor Author

bkhouri commented Mar 28, 2025

@swift-ci please test windows

@bkhouri bkhouri marked this pull request as draft May 1, 2025 19:16
@bkhouri
Copy link
Contributor Author

bkhouri commented May 4, 2025

@swift-ci test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test suite improvements to SwiftPM test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants