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 9*4add8c19SDouglas Jose## Installing `wasm32` Targets 10*4add8c19SDouglas Jose 11*4add8c19SDouglas JoseTo compile the tests, you'll need the `wasm32-wasi` and 12*4add8c19SDouglas Jose`wasm32-unknown-unknown` targets installed, which, assuming you're using 13*4add8c19SDouglas Jose[rustup.rs](https://rustup.rs) to manage your Rust versions, can be done as 14*4add8c19SDouglas Josefollows: 15*4add8c19SDouglas Jose 16*4add8c19SDouglas Jose```shell 17*4add8c19SDouglas Joserustup target add wasm32-wasi wasm32-unknown-unknown 18*4add8c19SDouglas Jose``` 19*4add8c19SDouglas Jose 20b74b0bc4SNick Fitzgerald## Running All Tests 21b74b0bc4SNick Fitzgerald 22*4add8c19SDouglas JoseTo run all of Wasmtime's tests, execute this command: 23b74b0bc4SNick Fitzgerald 24b74b0bc4SNick Fitzgerald```shell 25*4add8c19SDouglas Josecargo test --workspace 26570bd7ecSJakub Konka``` 27570bd7ecSJakub Konka 28b74b0bc4SNick FitzgeraldYou can also exclude a particular crate from testing with `--exclude`. For 29b74b0bc4SNick Fitzgeraldexample, if you want to avoid testing the `wastime-fuzzing` crate — which 30b74b0bc4SNick Fitzgeraldrequires that `libclang` is installed on your system, and for some reason maybe 31b74b0bc4SNick Fitzgeraldyou don't have it — you can run: 32b74b0bc4SNick Fitzgerald 33b74b0bc4SNick Fitzgerald```shell 34*4add8c19SDouglas Josecargo test --workspace --exclude wasmtime-fuzzing 35*4add8c19SDouglas Jose``` 36*4add8c19SDouglas Jose 37*4add8c19SDouglas JoseSimilarly, to skip WASI integration tests, run: 38*4add8c19SDouglas Jose 39*4add8c19SDouglas Jose```shell 40*4add8c19SDouglas Josecargo test --workspace --exclude test-programs 41b74b0bc4SNick Fitzgerald``` 42b74b0bc4SNick Fitzgerald 43b74b0bc4SNick Fitzgerald## Testing a Specific Crate 44b74b0bc4SNick Fitzgerald 45b74b0bc4SNick FitzgeraldYou can test a particular Wasmtime crate with `cargo test -p 46b74b0bc4SNick Fitzgeraldwasmtime-whatever`. For example, to test the `wasmtime-environ` crate, execute 47b74b0bc4SNick Fitzgeraldthis command: 48b74b0bc4SNick Fitzgerald 49b74b0bc4SNick Fitzgerald```shell 50b74b0bc4SNick Fitzgeraldcargo test -p wasmtime-environ 51b74b0bc4SNick Fitzgerald``` 52b74b0bc4SNick Fitzgerald 53b74b0bc4SNick FitzgeraldAlternatively, you can `cd` into the crate's directory, and run `cargo test` 54b74b0bc4SNick Fitzgeraldthere, without needing to supply the `-p` flag: 55b74b0bc4SNick Fitzgerald 56b74b0bc4SNick Fitzgerald```shell 57b74b0bc4SNick Fitzgeraldcd crates/environ/ 58b74b0bc4SNick Fitzgeraldcargo test 59b74b0bc4SNick Fitzgerald``` 60b74b0bc4SNick Fitzgerald 61b74b0bc4SNick Fitzgerald## Running the Wasm Spec Tests 62b74b0bc4SNick Fitzgerald 63b74b0bc4SNick FitzgeraldThe spec testsuite itself is in a git submodule, so make sure you've 64b74b0bc4SNick Fitzgeraldchecked it out and initialized its submodule: 65b74b0bc4SNick Fitzgerald 66b74b0bc4SNick Fitzgerald```shell 67b74b0bc4SNick Fitzgeraldgit submodule update --init 68b74b0bc4SNick Fitzgerald``` 69b74b0bc4SNick Fitzgerald 70b74b0bc4SNick FitzgeraldWhen the submodule is checked out, Wasmtime runs the Wasm spec testsuite as part 71b74b0bc4SNick Fitzgeraldof testing the `wasmtime-cli` crate: 72b74b0bc4SNick Fitzgerald 73b74b0bc4SNick Fitzgerald```shell 74b74b0bc4SNick Fitzgeraldcargo test -p wasmtime-cli 75b74b0bc4SNick Fitzgerald``` 76b74b0bc4SNick Fitzgerald 77570bd7ecSJakub Konka## Running WASI Integration Tests Only 78570bd7ecSJakub Konka 79570bd7ecSJakub KonkaWASI integration tests can be run separately from all other tests which 80570bd7ecSJakub Konkacan be useful when working on the `wasi-common` crate. This can be done by 81570bd7ecSJakub Konkaexecuting this command: 82570bd7ecSJakub Konka 83570bd7ecSJakub Konka```shell 84*4add8c19SDouglas Josecargo test -p test-programs 85570bd7ecSJakub Konka``` 86570bd7ecSJakub Konka 87b74b0bc4SNick Fitzgerald## Adding New Tests 88b74b0bc4SNick Fitzgerald 89b74b0bc4SNick Fitzgerald### Adding Rust's `#[test]`-Style Tests 90b74b0bc4SNick Fitzgerald 91b74b0bc4SNick FitzgeraldFor very "unit-y" tests, we add `test` modules in the same `.rs` file as the 92b74b0bc4SNick Fitzgeraldcode that is being tested. These `test` modules are configured to only get 93b74b0bc4SNick Fitzgeraldcompiled during testing with `#[cfg(test)]`. 94b74b0bc4SNick Fitzgerald 95b74b0bc4SNick Fitzgerald```rust 96b74b0bc4SNick Fitzgerald// some code... 97b74b0bc4SNick Fitzgerald 98b74b0bc4SNick Fitzgerald#[cfg(test)] 99b74b0bc4SNick Fitzgeraldmod tests { 100b74b0bc4SNick Fitzgerald use super::*; 101b74b0bc4SNick Fitzgerald 102b74b0bc4SNick Fitzgerald #[test] 103b74b0bc4SNick Fitzgerald fn some_test_for_that_code() { 104b74b0bc4SNick Fitzgerald // ... 105b74b0bc4SNick Fitzgerald } 106b74b0bc4SNick Fitzgerald} 107b74b0bc4SNick Fitzgerald``` 108b74b0bc4SNick Fitzgerald 109b74b0bc4SNick FitzgeraldIf you're writing a unit test and a `test` module doesn't already exist, you can 110b74b0bc4SNick Fitzgeraldcreate one. 111b74b0bc4SNick Fitzgerald 112b74b0bc4SNick FitzgeraldFor more "integration-y" tests, we create a `tests` directory within the crate, 113b74b0bc4SNick Fitzgeraldand put the tests inside there. For example, there are various code 114b74b0bc4SNick Fitzgeraldcache-related tests at `crates/environ/tests/cache_*.rs`. Always feel free to 115b74b0bc4SNick Fitzgeraldadd a `tests` directory to a crate, if you want to add a new test and there 116b74b0bc4SNick Fitzgeraldaren't any existing tests. 117b74b0bc4SNick Fitzgerald 118b74b0bc4SNick Fitzgerald### Adding Specification-Style Wast Tests 119b74b0bc4SNick Fitzgerald 120b74b0bc4SNick FitzgeraldWe use the spec testsuite as-is and without custom patches or a forked 12106280401SNick Fitzgeraldversion. This probably isn't what you want to modify when adding a new Wasmtime 12206280401SNick Fitzgeraldtest! 12306280401SNick Fitzgerald 12406280401SNick FitzgeraldWhen you have a Wasmtime-specific test that you'd like to write in Wast and use 12506280401SNick Fitzgeraldthe Wast-style assertions, you can add it to our "misc testsuite". The misc 12606280401SNick Fitzgeraldtestsuite uses the same syntax and assertions as the spec testsuite, but lives 12706280401SNick Fitzgeraldin `tests/misc_testsuite`. Feel free to add new tests to existing 12806280401SNick Fitzgerald`tests/misc_testsuite/*.wast` files or create new ones as needed. These tests 12906280401SNick Fitzgeraldare run as part of the `wasmtime-cli` crate's tests. 13006280401SNick Fitzgerald 13106280401SNick FitzgeraldIf you have a new test that you think really belongs in the spec testsuite, make 13206280401SNick Fitzgeraldsure it makes sense for every Wasm implementation to run your test (i.e. it 13306280401SNick Fitzgeraldisn't Wasmtime-specific) and send a pull request 134b74b0bc4SNick Fitzgerald[upstream](https://github.com/WebAssembly/testsuite/). Once it is accepted in 135b74b0bc4SNick Fitzgeraldthe upstream repo, we can update our git submodule and we'll start running the 136b74b0bc4SNick Fitzgeraldnew tests. 137570bd7ecSJakub Konka 138570bd7ecSJakub Konka### Adding WASI Integration Tests 139570bd7ecSJakub Konka 140570bd7ecSJakub KonkaWhen you have a WASI-specific test program that you'd like to include as a 141570bd7ecSJakub Konkatest case to run against our WASI implementation, you can add it to our 142570bd7ecSJakub Konka`test-programs` crate. In particular, you should drop a main-style Rust source 143570bd7ecSJakub Konkafile into `crates/test-programs/wasi-tests/src/bin/some_new_test.rs` with a 144570bd7ecSJakub Konkaname of your choice. And that's it! The build script included in the 145570bd7ecSJakub Konka`test-programs` crate will automatically generate the necessary boilerplate 146570bd7ecSJakub Konkacode for your test program so that it's run on all supported hosts. 147570bd7ecSJakub Konka 148570bd7ecSJakub KonkaIf you would like to tweak which host to run the test program against however 149570bd7ecSJakub Konka(for instance, only on Unix but on Windows), you can tweak that in the build 150570bd7ecSJakub Konkascript located under `crates/test-programs/build.rs`. 151