lang_tester
This crate provides a simple language testing framework designed to help when
you are testing things like compilers and virtual machines. It allows users to
embed simple tests for process success/failure and for stderr/stdout inside a
source file. It is loosely based on the
compiletest_rs
crate, but is much
simpler (and hence sometimes less powerful), and designed to be used for
testing non-Rust languages too.
For example, a Rust language tester, loosely in the spirit of
compiletest_rs
, looks as follows:
use ;
use LangTester;
use TempDir;
This defines a lang tester that uses all *.rs
files in a given directory as
test files, running two test commands against them: Compiler
(i.e. rustc
);
and Run-time
(the compiled binary).
Users can then write test files such as the following:
// Compiler:
// stderr:
// warning: unused variable: `x`
// ...unused_var.rs:12:9
// ...
//
// Run-time:
// stdout: Hello world
Test data is specified with a two-level indentation syntax: the outer most level of indentation defines a test command (multiple command names can be specified, as in the above); the inner most level of indentation defines alterations to the general command or sub-tests. Each test command must define at least one sub-test:
status: <success|failure|<int>>
, wheresuccess
andfailure
map to platform specific notions of a command completing successfully or unsuccessfully respectively and<int>
is a signed integer checking for a specific exit code on platforms that support it. If not specified, defaults tosuccess
.stderr: [<string>]
,stdout: [<string>]
are matched strings against a command'sstderr
orstdout
. The special string...
can be used as a simple wildcard: if a line consists solely of...
, it means "match zero or more lines"; if a line begins with...
, it means "match the remainder of the line only"; if a line ends with...
, it means "match the start of the line only". A line may start and end with...
. Note thatstderr
/stdout
matches ignore leading/trailing whitespace and newlines, but are case sensitive. If not specified, defaults to...
(i.e. match anything). Note that the empty string matches only the empty string so e.g.stderr:
on its own means that a command'sstderr
muct not contain any output.
Test commands can alter the general command by specifying zero or more of the following:
extra-args: <arg 1> [... <arg n>]
, where each space separated argument will be appended, in order, to those arguments specified as part of thetest_cmds
function.
The above file thus contains 4 meaningful tests, two specified by the user and
two implied by defaults: the Compiler
should succeed (e.g. return a 0
exit
code when run on Unix), and its stderr
output should warn about an unused
variable on line 12; and the resulting binary should succeed produce Hello world
on stdout
.
lang_tester
's output is deliberately similar to Rust's normal testing output.
Running the example rust_lang_tester
in this crate produces the following
output:
$ cargo run --example=rust_lang_tester
Compiling lang_tester v0.1.0 (/home/ltratt/scratch/softdev/lang_tester)
Finished dev [unoptimized + debuginfo] target(s) in 3.49s
Running `target/debug/examples/rust_lang_tester`
running 4 tests
test lang_tests::no_main ... ok
test lang_tests::unknown_var ... ok
test lang_tests::unused_var ... ok
test lang_tests::exit_code ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
If you want to run a subset of tests, you can specify simple filters which use substring match to run a subset of tests:
$ cargo run --example=rust_lang_tester var
Compiling lang_tester v0.1.0 (/home/ltratt/scratch/softdev/lang_tester)
Finished dev [unoptimized + debuginfo] target(s) in 3.37s
Running `target/debug/examples/rust_lang_tester var`
running 2 tests
test lang_tests::unknown_var ... ok
test lang_tests::unused_var ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out
Integration with Cargo.
Tests created with lang_tester can be used as part of an existing test suite and
can be run with the cargo test
command. For example, if the Rust source file
that runs your lang tests is lang_tests/run.rs
then add the following to your
Cargo.toml:
[[test]]
name = "lang_tests"
path = "lang_tests/run.rs"
harness = false