1b74b0bc4SNick Fitzgerald# Testing 2b74b0bc4SNick Fitzgerald 3b74b0bc4SNick FitzgeraldThis section describes how to run Wasmtime's tests and add new tests. 4b74b0bc4SNick Fitzgerald 5b74b0bc4SNick FitzgeraldBefore continuing, make sure you can [build 63848bf54STakanori IshibashiWasmtime](./contributing-building.md) successfully. Can't run the tests if you 7b74b0bc4SNick Fitzgeraldcan't build it! 8b74b0bc4SNick Fitzgerald 94add8c19SDouglas Jose## Installing `wasm32` Targets 104add8c19SDouglas Jose 1105095c18SAlex CrichtonTo compile the tests, you'll need the `wasm32-wasip1` and 124add8c19SDouglas Jose`wasm32-unknown-unknown` targets installed, which, assuming you're using 134add8c19SDouglas Jose[rustup.rs](https://rustup.rs) to manage your Rust versions, can be done as 144add8c19SDouglas Josefollows: 154add8c19SDouglas Jose 1692cfda1bSVictor Adossi```console 1705095c18SAlex Crichtonrustup target add wasm32-wasip1 wasm32-unknown-unknown 184add8c19SDouglas Jose``` 194add8c19SDouglas Jose 20459378c7SAlex Crichton## Running Tests 21b74b0bc4SNick Fitzgerald 22459378c7SAlex CrichtonDepending on what you're modifying there's a few commands you may be the most 23459378c7SAlex Crichtoninterested: 24459378c7SAlex Crichton 25459378c7SAlex Crichton* `cargo test` - used to run the `tests/*` folder at the top-level. This tests 26459378c7SAlex Crichton the CLI and contains most tests for the `wasmtime` crate itself. This will 27459378c7SAlex Crichton also run all spec tests. Note that this does not run all tests in the 28459378c7SAlex Crichton repository, but it's generally a good starting point. 29459378c7SAlex Crichton* `cargo test -p cranelift-tools` - used if you're working on Cranelift and this 30459378c7SAlex Crichton will run all the tests at `cranelift/filetests/filetests`. You can also, 31459378c7SAlex Crichton within the `cranelift` folder, run `cargo run test ./filetests` to run these 32459378c7SAlex Crichton tests. 33459378c7SAlex Crichton* `cargo test -p wasmtime-wasi` - this will run all WASI tests for the 34459378c7SAlex Crichton `wasmtime-wasi` crate. 35459378c7SAlex Crichton 36459378c7SAlex CrichtonAt this time not all of the crates in the Wasmtime workspace can be tested, so 37459378c7SAlex Crichtonrunning all tests is a little non-standard. To match what CI does and run all 38459378c7SAlex Crichtontests you'll need to execute 39b74b0bc4SNick Fitzgerald 4092cfda1bSVictor Adossi```console 41*f5fd058bSJerry Yang./ci/run-tests.py 42b74b0bc4SNick Fitzgerald``` 43b74b0bc4SNick Fitzgerald 44b74b0bc4SNick Fitzgerald## Testing a Specific Crate 45b74b0bc4SNick Fitzgerald 46b74b0bc4SNick FitzgeraldYou can test a particular Wasmtime crate with `cargo test -p 47b74b0bc4SNick Fitzgeraldwasmtime-whatever`. For example, to test the `wasmtime-environ` crate, execute 48b74b0bc4SNick Fitzgeraldthis command: 49b74b0bc4SNick Fitzgerald 5092cfda1bSVictor Adossi```console 51b74b0bc4SNick Fitzgeraldcargo test -p wasmtime-environ 52b74b0bc4SNick Fitzgerald``` 53b74b0bc4SNick Fitzgerald 54b74b0bc4SNick FitzgeraldAlternatively, you can `cd` into the crate's directory, and run `cargo test` 55b74b0bc4SNick Fitzgeraldthere, without needing to supply the `-p` flag: 56b74b0bc4SNick Fitzgerald 5792cfda1bSVictor Adossi```console 58b74b0bc4SNick Fitzgeraldcd crates/environ/ 59b74b0bc4SNick Fitzgeraldcargo test 60b74b0bc4SNick Fitzgerald``` 61b74b0bc4SNick Fitzgerald 62b74b0bc4SNick Fitzgerald## Running the Wasm Spec Tests 63b74b0bc4SNick Fitzgerald 64b74b0bc4SNick FitzgeraldThe spec testsuite itself is in a git submodule, so make sure you've 65b74b0bc4SNick Fitzgeraldchecked it out and initialized its submodule: 66b74b0bc4SNick Fitzgerald 6792cfda1bSVictor Adossi```console 68b74b0bc4SNick Fitzgeraldgit submodule update --init 69b74b0bc4SNick Fitzgerald``` 70b74b0bc4SNick Fitzgerald 71b74b0bc4SNick FitzgeraldWhen the submodule is checked out, Wasmtime runs the Wasm spec testsuite as part 72459378c7SAlex Crichtonof testing the `wasmtime-cli` crate at the crate root, meaning in the root of 73459378c7SAlex Crichtonthe repository you can execute: 74b74b0bc4SNick Fitzgerald 7592cfda1bSVictor Adossi```console 76459378c7SAlex Crichtoncargo test --test wast 77b74b0bc4SNick Fitzgerald``` 78b74b0bc4SNick Fitzgerald 79459378c7SAlex CrichtonYou can pass an additional CLI argument to act as a filter on which tests to 80459378c7SAlex Crichtonrun. For example to only run the spec tests themselves (excluding handwritten 81459378c7SAlex CrichtonWasmtime-specific tests) and only in Cranelift you can run: 82459378c7SAlex Crichton 8392cfda1bSVictor Adossi```console 84459378c7SAlex Crichtoncargo test --test wast Cranelift/tests/spec 85459378c7SAlex Crichton``` 86459378c7SAlex Crichton 87459378c7SAlex CrichtonNote that in general spec tests are executed regardless of whether they pass 88459378c7SAlex Crichtonor not. In `tests/wast.rs` there's a `should_fail` function which indicates the 89459378c7SAlex Crichtonexpected result of the test. When adding new spec tests or implementing features 90459378c7SAlex Crichtonthis function will need to be updated as tests change from failing to passing. 91459378c7SAlex Crichton 92459378c7SAlex Crichton## Running WASI Integration Tests 93570bd7ecSJakub Konka 94570bd7ecSJakub KonkaWASI integration tests can be run separately from all other tests which 95459378c7SAlex Crichtoncan be useful when working on the `wasmtime-wasi` crate. This can be done by 96570bd7ecSJakub Konkaexecuting this command: 97570bd7ecSJakub Konka 9892cfda1bSVictor Adossi```console 99459378c7SAlex Crichtoncargo test -p wasmtime-wasi 100570bd7ecSJakub Konka``` 101570bd7ecSJakub Konka 102459378c7SAlex CrichtonSimilarly if you're testing HTTP-related functionality you can execute: 103459378c7SAlex Crichton 10492cfda1bSVictor Adossi```console 105459378c7SAlex Crichtoncargo test -p wasmtime-wasi-http 106459378c7SAlex Crichton``` 107459378c7SAlex Crichton 108459378c7SAlex CrichtonNote that these tests will compile programs in `crates/test-programs` to run. 109459378c7SAlex Crichton 110b74b0bc4SNick Fitzgerald## Adding New Tests 111b74b0bc4SNick Fitzgerald 112b74b0bc4SNick Fitzgerald### Adding Rust's `#[test]`-Style Tests 113b74b0bc4SNick Fitzgerald 114b74b0bc4SNick FitzgeraldFor very "unit-y" tests, we add `test` modules in the same `.rs` file as the 115b74b0bc4SNick Fitzgeraldcode that is being tested. These `test` modules are configured to only get 116b74b0bc4SNick Fitzgeraldcompiled during testing with `#[cfg(test)]`. 117b74b0bc4SNick Fitzgerald 118b74b0bc4SNick Fitzgerald```rust 119b74b0bc4SNick Fitzgerald// some code... 120b74b0bc4SNick Fitzgerald 121b74b0bc4SNick Fitzgerald#[cfg(test)] 122b74b0bc4SNick Fitzgeraldmod tests { 123b74b0bc4SNick Fitzgerald use super::*; 124b74b0bc4SNick Fitzgerald 125b74b0bc4SNick Fitzgerald #[test] 126b74b0bc4SNick Fitzgerald fn some_test_for_that_code() { 127b74b0bc4SNick Fitzgerald // ... 128b74b0bc4SNick Fitzgerald } 129b74b0bc4SNick Fitzgerald} 130b74b0bc4SNick Fitzgerald``` 131b74b0bc4SNick Fitzgerald 132b74b0bc4SNick FitzgeraldIf you're writing a unit test and a `test` module doesn't already exist, you can 133b74b0bc4SNick Fitzgeraldcreate one. 134b74b0bc4SNick Fitzgerald 135459378c7SAlex CrichtonFor more "integration-y" tests, each crate supports a separate `tests` directory 136459378c7SAlex Crichtonwithin the crate, and put the tests inside there. Most integration tests in 137459378c7SAlex CrichtonWasmtime are located in the root `tests/*.rs` location, notably 138459378c7SAlex Crichton`tests/all/*.rs`. This tests much of the `wasmtime` crate for example and 139459378c7SAlex Crichtonfacilitates `cargo test` at the repository root running most tests. 140459378c7SAlex Crichton 141459378c7SAlex CrichtonSome tests make more sense to live per-crate, though. For example, many WASI 142459378c7SAlex Crichtontests are at `crates/wasi/tests/*.rs`. For adding a test feel free to add it 143459378c7SAlex Crichtonwherever feels best, there's not really a strong reason to put it in one place 144459378c7SAlex Crichtonover another. While it's easiest to add to existing tests it's ok to add a new 145459378c7SAlex Crichton`tests` directory with tests too. 146b74b0bc4SNick Fitzgerald 147b74b0bc4SNick Fitzgerald### Adding Specification-Style Wast Tests 148b74b0bc4SNick Fitzgerald 149b74b0bc4SNick FitzgeraldWe use the spec testsuite as-is and without custom patches or a forked 150459378c7SAlex Crichtonversion via a submodule at `tests/spec_testsuite`. This probably isn't what you 151459378c7SAlex Crichtonwant to modify when adding a new Wasmtime test! 15206280401SNick Fitzgerald 15306280401SNick FitzgeraldWhen you have a Wasmtime-specific test that you'd like to write in Wast and use 15406280401SNick Fitzgeraldthe Wast-style assertions, you can add it to our "misc testsuite". The misc 15506280401SNick Fitzgeraldtestsuite uses the same syntax and assertions as the spec testsuite, but lives 15606280401SNick Fitzgeraldin `tests/misc_testsuite`. Feel free to add new tests to existing 15706280401SNick Fitzgerald`tests/misc_testsuite/*.wast` files or create new ones as needed. These tests 158459378c7SAlex Crichtonare run from the crate root: 159459378c7SAlex Crichton 16092cfda1bSVictor Adossi```console 161459378c7SAlex Crichtoncargo test --test wast 162459378c7SAlex Crichton``` 16306280401SNick Fitzgerald 16406280401SNick FitzgeraldIf you have a new test that you think really belongs in the spec testsuite, make 16506280401SNick Fitzgeraldsure it makes sense for every Wasm implementation to run your test (i.e. it 16606280401SNick Fitzgeraldisn't Wasmtime-specific) and send a pull request 167459378c7SAlex Crichton[upstream](https://github.com/WebAssembly/spec). Once it is accepted in the 168459378c7SAlex Crichtonupstream repo, it'll make its way to the test-specific mirror at 169459378c7SAlex Crichton[WebAssembly/testsuite](https://github.com/WebAssembly/testsuite) and then we 170459378c7SAlex Crichtoncan update our git submodule and we'll start running the new tests. 171570bd7ecSJakub Konka 172570bd7ecSJakub Konka### Adding WASI Integration Tests 173570bd7ecSJakub Konka 174570bd7ecSJakub KonkaWhen you have a WASI-specific test program that you'd like to include as a 175570bd7ecSJakub Konkatest case to run against our WASI implementation, you can add it to our 176570bd7ecSJakub Konka`test-programs` crate. In particular, you should drop a main-style Rust source 177459378c7SAlex Crichtonfile into `crates/test-programs/src/bin/PREFIX_some_new_test.rs`. Here the 178459378c7SAlex Crichton`PREFIX` indicates what test suite it's going to run as. For example 179459378c7SAlex Crichton`preview2_*` tests are run as part of `wasmtime-wasi` crate tests. The `cli_*` 180459378c7SAlex Crichtontests are run as part of `tests/all/cli_tests.rs`. It's probably easiest to use 181459378c7SAlex Crichtona preexisting prefix. The `some_new_test` name is arbitrary and is selected as 182459378c7SAlex Crichtonappropriate by you. 183570bd7ecSJakub Konka 184459378c7SAlex CrichtonOne a test file is added you'll need to add some code to execute the tests as 185459378c7SAlex Crichtonwell. For example if you added a new test 186459378c7SAlex Crichton`crates/test-programs/src/bin/cli_my_test.rs` then you'll need to add a new 187459378c7SAlex Crichtonfunction to `tests/all/cli_tests.rs` such as: 188459378c7SAlex Crichton 189459378c7SAlex Crichton```rust 190459378c7SAlex Crichton#[test] 191459378c7SAlex Crichtonfn my_test() { 192459378c7SAlex Crichton // ... 193459378c7SAlex Crichton} 194459378c7SAlex Crichton``` 195459378c7SAlex Crichton 196459378c7SAlex CrichtonThe path to the compiled WebAssembly of your test case will be available in a 197459378c7SAlex CrichtonRust-level `const` named `CLI_MY_TEST`. There is also a component version at 198459378c7SAlex Crichton`CLI_MY_TEST_COMPONENT`. These are used to pass as arguments to 199459378c7SAlex Crichton`wasmtime`-the-CLI for example or you can use `Module::from_file`. 200459378c7SAlex Crichton 201459378c7SAlex CrichtonWhen in doubt feel free to copy existing tests and then modify them to suit your 202459378c7SAlex Crichtonneeds. 203