|
1 | 1 | # Testing your app |
2 | 2 |
|
3 | | -You can write a test suite for your SwiftWasm app or library, or run an existing test suite |
4 | | -written for `XCTest` if you port existing code to SwiftWasm. Your project has to have a |
5 | | -`Package.swift` package manifest for this to work. We assume that you use SwiftPM to build your |
6 | | -project and that you have a working package manifest. Please follow [our SwiftPM guide](./swift-package.md) for new projects. |
| 3 | +SwiftWasm supports both `swift-testing` and `XCTest` for writing test suites. Your project needs to have a |
| 4 | +`Package.swift` package manifest with test targets configured. Please follow [our SwiftPM guide](./swift-package.md) for new projects. |
7 | 5 |
|
8 | | -## A simple test case |
| 6 | +## Testing in JavaScript environments |
9 | 7 |
|
10 | | -Let's assume you have an `Example` target in your project that you'd like to test. Your |
11 | | -`Package.swift` should also have a test suite target with a dependency on the library target. It |
12 | | -would probably look like this: |
| 8 | +If you're building a SwiftWasm app that runs in JavaScript environments (browsers or Node.js), you can use JavaScriptKit's testing utilities to run your tests directly in those environments. For detailed information on how to set up and run tests in JavaScript environments, please refer to the [JavaScriptKit Testing documentation](https://swiftpackageindex.com/swiftwasm/javascriptkit/documentation/javascriptkit/testing). |
13 | 9 |
|
14 | | -```swift |
15 | | -// swift-tools-version: 5.9 |
16 | | - |
17 | | -import PackageDescription |
18 | | - |
19 | | -let package = Package( |
20 | | - name: "Example", |
21 | | - products: [ |
22 | | - .library(name: "Example", targets: ["Example"]), |
23 | | - ], |
24 | | - targets: [ |
25 | | - .target(name: "Example"), |
26 | | - .testTarget(name: "ExampleTests", dependencies: ["Example"]), |
27 | | - ] |
28 | | -) |
29 | | -``` |
| 10 | +## Standalone testing with WASI |
| 11 | + |
| 12 | +If you prefer to run tests in a standalone environment without JavaScript, you can use WASI-compatible runtimes as described below. |
30 | 13 |
|
31 | | -Now you should make sure there's `Tests/ExampleTests` subdirectory in your project. |
32 | | -If you don't have any files in it yet, create `ExampleTests.swift` in it: |
| 14 | +Make sure your `Package.swift` has test targets configured. For example: |
| 15 | + |
| 16 | +Note that `swift test` doesn't work for WebAssembly targets. Building tests and running them are two separate steps. After building your tests, you can use a WASI-compatible host such as [wasmtime](https://wasmtime.dev/) or [WasmKit](https://github.com/swiftwasm/WasmKit) to run the test bundle. |
33 | 17 |
|
34 | 18 | ```swift |
35 | | -import Example |
36 | | -import XCTest |
37 | | - |
38 | | -final class ExampleTests: XCTestCase { |
39 | | - func testTrivial() { |
40 | | - XCTAssertEqual(text, "Hello, world") |
41 | | - } |
42 | | -} |
| 19 | +targets: [ |
| 20 | + .target(name: "Example"), |
| 21 | + .testTarget(name: "ExampleTests", dependencies: ["Example"]), |
| 22 | +] |
43 | 23 | ``` |
44 | 24 |
|
45 | | -This code assumes that your `Example` defines some `text` with `"Hello, world"` value |
46 | | -for this test to pass. Your test functions should all start with `test`, please see [XCTest |
47 | | -documentation](https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods) |
48 | | -for more details. |
| 25 | +### Running swift-testing suites |
| 26 | + |
| 27 | +For running `swift-testing` test suites, please refer to the [swift-testing WASI documentation](https://github.com/swiftlang/swift-testing/blob/main/Documentation/WASI.md). |
49 | 28 |
|
50 | | -## Building and running the test suite with `SwiftPM` |
| 29 | +### Running XCTest suites |
51 | 30 |
|
52 | | -You can build your test suite by running this command in your terminal: |
| 31 | +You can build your XCTest suite by running this command in your terminal: |
53 | 32 |
|
54 | 33 | ```sh |
55 | | -$ swift build --build-tests --triple wasm32-unknown-wasi |
| 34 | +$ swift build --build-tests --swift-sdk $SWIFT_SDK_ID |
56 | 35 | ``` |
57 | 36 |
|
58 | | -If you're used to running `swift test` to run test suites for other Swift platforms, we have to |
59 | | -warn you that this won't work. `swift test` doesn't know what WebAssembly environment you'd like to |
60 | | -use to run your tests. Because of this building tests and running them are two separate steps when |
61 | | -using `SwiftPM`. After your tests are built, you can use a WASI-compatible host such as |
62 | | -[wasmtime](https://wasmtime.dev/) to run the test bundle: |
| 37 | +After building, run the test bundle with a WASI runtime. For example, with wasmtime: |
63 | 38 |
|
64 | 39 | ```sh |
65 | | -$ wasmtime --dir . .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm |
| 40 | +$ wasmtime --dir . .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm |
| 41 | +``` |
| 42 | + |
| 43 | +Or with WasmKit: |
| 44 | + |
| 45 | +```sh |
| 46 | +$ wasmkit run --dir . .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm |
66 | 47 | ``` |
67 | 48 |
|
68 | 49 | (`--dir .` is used to allow XCTest to find `Bundle.main` resources placed alongside the executable file.) |
69 | 50 |
|
70 | 51 | As you can see, the produced test binary starts with the name of your package followed by |
71 | | -`PackageTests.wasm`. It is located in the `.build/debug` subdirectory, or in the `.build/release` |
| 52 | +`PackageTests.wasm`. It is located in the `.build/wasm32-unknown-wasip1/debug` subdirectory, or in the `.build/wasm32-unknown-wasip1/release` |
72 | 53 | subdirectory when you build in release mode. |
73 | 54 |
|
74 | | -## Code coverage with `SwiftPM` |
| 55 | +### Code coverage |
75 | 56 |
|
76 | 57 | > **Note**: Code coverage support is available only in 6.1 and later. |
77 | 58 |
|
78 | 59 | You can also generate code coverage reports for your test suite. To do this, you need to build your |
79 | 60 | test suite with the `--enable-code-coverage` and linker options `-Xlinker -lwasi-emulated-getpid`: |
80 | 61 |
|
81 | 62 | ```sh |
82 | | -$ swift build --build-tests --swift-sdk wasm32-unknown-wasi --enable-code-coverage -Xlinker -lwasi-emulated-getpid |
| 63 | +$ swift build --build-tests --swift-sdk $SWIFT_SDK_ID --enable-code-coverage -Xlinker -lwasi-emulated-getpid |
83 | 64 | ``` |
84 | 65 |
|
85 | | -After building your test suite, you can run it with `wasmtime` as described above. The raw coverage |
| 66 | +After building your test suite, you can run it with a WASI runtime (e.g., `wasmtime` or `WasmKit`) as described above. The raw coverage |
86 | 67 | data will be stored in `default.profraw` file in the current directory. You can use the `llvm-profdata` |
87 | 68 | and `llvm-cov` tools to generate a human-readable report: |
88 | 69 |
|
89 | 70 | ```sh |
90 | | -$ wasmtime --dir . .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm |
| 71 | +$ wasmtime --dir . .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm |
91 | 72 | $ llvm-profdata merge default.profraw -o default.profdata |
92 | | -$ llvm-cov show .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm -instr-profile=default.profdata |
| 73 | +$ llvm-cov show .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm -instr-profile=default.profdata |
93 | 74 | # or generate an HTML report |
94 | | -$ llvm-cov show .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm -instr-profile=default.profdata --format=html -o coverage |
| 75 | +$ llvm-cov show .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm -instr-profile=default.profdata --format=html -o coverage |
95 | 76 | $ open coverage/index.html |
96 | 77 | ``` |
97 | 78 |
|
98 | 79 |  |
99 | | - |
100 | | -## Building and running the test suite with `carton` |
101 | | - |
102 | | -If you use [`carton`](https://carton.dev) to develop and build your app, as described in [our guide |
103 | | -for browser apps](./browser-app.md), just run `swift run carton test` in the |
104 | | -root directory of your package. This will automatically build the test suite and run it with a WASI runtime for you. |
0 commit comments