• Stars
    star
    454
  • Rank 96,373 (Top 2 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Organized, flexible testing framework for Rust

Stainless Build Status

Stainless is a lightweight, flexible, unopinionated testing framework.

Note that stainless currently requires the nightly version of the Rust compiler!

This project is unmaintained

Users are advised to use speculate.rs instead which implements a similar idea.

Installation

Add stainless as a dependency in your Cargo.toml file

[dev-dependencies]
stainless = "*"

Add the following lines to the top of your root module. That file is normally called src/main.rs for executables and src/lib.rs for libraries:

#![feature(plugin)]
#![cfg_attr(test, plugin(stainless))]

This will make stainless available when you run the tests using cargo test. When using stainless only with a library, make sure to run tests using cargo test --lib.

Overview

Stainless exports the describe! syntax extension, which allows you to quickly generate complex testing hierarchies and reduce boilerplate through before_each and after_each.

Stainless currently supports the following types of subblocks:

  • before_each and after_each
  • it, failing, and ignore
  • bench
  • nested describe!

before_each and after_each allow you to group common initialization and teardown for a group of tests into a single block, shortening your tests.

it generates tests which use before_each and after_each. failing does the same, except the generated tests are marked with #[should_panic]. It optionally takes an argument which is matched against the failure message. ignore is equivalent to marking a test with #[ignore] which disables the test by default.

bench allows you to generate benchmarks in the same fashion, though before_each and after_each blocks do not currently affect bench blocks.

Nested describe! blocks allow you to better organize your tests into small units and gives you granular control over where before_each and after_each apply. Of course the before_each and after_each blocks of the wrapping describe! blocks are executed as well.

Together, these 4 types of subblocks give you more flexibility and control than the built in testing infrastructure.

Example

describe! stainless {
    before_each {
        // Start up a test.
        let mut stainless = true;
    }

    it "makes organizing tests easy" {
        // Do the test.
        assert!(stainless);
    }

    after_each {
        // End the test.
        stainless = false;
    }

    bench "something simple" (bencher) {
        bencher.iter(|| 2 * 2)
    }

    describe! nesting {

        before_each {
          let mut inner_stainless = true;
        }

        after_each {
          inner_stainless = false;
        }

        it "makes it simple to categorize tests" {
            // It even generates submodules!
            assert_eq!(2, 2);
        }
    }
}

Expands to (roughly):

mod stainless {
    #[test]
    fn makes_organizing_tests_easy() {
        let mut stainless = true;
        assert!(stainless);
        stainless = false;
    }

    #[bench]
    fn something_simple(bencher: &mut test::Bencher) {
        bencher.iter(|| 2 * 2)
    }

    mod nesting {
        #[test]
        fn makes_it_simple_to_categorize_tests() {
            let mut stainless = true;
            let mut inner_stainless = true;
            assert_eq!(2, 2);
            inner_stainless = false;
            stainless = false;
        }
    }
}

Importing modules

At this point it is not possible to put use statements inside the describe! blocks. To allow usage of data structures from other modules and crates each describe! block comes with a silent pub use super::*; in it. That way everything you pub use in the containing module is available in your tests.

#[cfg(test)]
mod tests {
    pub use std::collections::HashMap;

    describe! stainless {
        it "can use HashMap" {
            let map = HashMap::new();
        }
    }
}

License

MIT. See the LICENSE file for details.

Authors

See Cargo.toml for the full list of authors.

More Repositories

1

rust-ordered-float

Rust
184
star
2

rust-typemap

A typesafe store keyed by types and containing different types of values.
Rust
166
star
3

rust-lazy

Lazy Evaluation for Rust.
Rust
135
star
4

rust-event

A global event loop
Rust
109
star
5

rust-scoped-pool

A flexible thread pool providing scoped and static threads.
Rust
82
star
6

rust-void

The empty type Void.
Rust
69
star
7

rust-n-queens

A fast, parallel backtracking algorithm for n-queens implemented in rust.
Rust
67
star
8

rust-shared-mutex

A reader-writer lock that can be used with a Condvar.
Rust
29
star
9

rust-modifier

Convenient chaining APIs for free
Rust
29
star
10

rust-plugin

Type-Safe, Lazily Evaluated, Plugins for Extensible Types
Rust
26
star
11

rust-dynamic

A dynamically typed value with fast downcasting.
Rust
26
star
12

rust-n-tree

A quadtree-like structure of arbitrary arity
Rust
23
star
13

rust-traverse

Traversals in Rust
Rust
21
star
14

rust-error

An extensible, typesafe error for everyone.
Rust
20
star
15

interstellar

Fast Particle Engine built in 36 hours
JavaScript
19
star
16

rust-unreachable

Rust
19
star
17

rust-aio

Rust
17
star
18

rust-resistant-taskpool

A panic resistant task pool.
Rust
16
star
19

rust-inspect

A simple debugging library for rust in rust
Rust
15
star
20

transfer

Rust
15
star
21

rust-membuf

A safe-ish wrapper for allocating, reallocating and deallocating heap buffers.
Rust
12
star
22

rust-mac

A collection of great and ubiqutitous macros.
Rust
11
star
23

rust-typeable

Rust
11
star
24

rust-http2parse

Rust
11
star
25

d3-particle-simulator

A particle simulation implemented using d3.
JavaScript
10
star
26

watch

watch shell utility for triggering commands on file changes
Shell
9
star
27

rust-unsafe-any

Unsafe runtime downcasting for Rust
Rust
9
star
28

rust-debug-unreachable

unreachable!() in debug, std::intrinsics::unreachable() in release
Rust
8
star
29

rust-once-mutex

A mutex providing one-time synchronized access, then safe lock-free access.
Rust
7
star
30

rust-traitobject

Rust
7
star
31

rust-enforce

Rust
7
star
32

rust-pico

Rust
5
star
33

rust-memalloc

Raw allocation APIs in stable rust.
Rust
5
star
34

go-lazy

A Lazy type for synchronization of lazily-evaluated data.
Go
5
star
35

rust-emitter

Rust
4
star
36

rust-appendbuf

Rust
4
star
37

rust-briefly

Concise explanations of rust.
4
star
38

rust-intovec

Rust
4
star
39

rust-expect

A flexible library for adding fluid assertions to types.
Rust
3
star
40

rust-event-emitter

Rust
3
star
41

rust-replace-map

Replace the value at a mutable memory location using a closure
Rust
3
star
42

python-monad

Haskell monads in Python!
Python
3
star
43

data-structures

In Javascript
JavaScript
3
star
44

rust-lockfree

Rust
2
star
45

rust-or

Rust
2
star
46

rust-dbqueue

Rust
2
star
47

railgun

2
star
48

rust-from

Rust
2
star
49

rust-lazylist

Rust
2
star
50

rust-actor

Actors in Rust
Rust
2
star
51

rust-nullable

Rust
2
star
52

rust-http

Rust
2
star
53

go-once-mutex

A Mutex offering one-time locking then infinite, concurrent, lock-free reads.
Go
2
star
54

lithium

Rust
2
star
55

rust-atomic-option

Rust
2
star
56

rust-http-content-type

Rust
2
star
57

go-future

A condition-variable based Future type for synchronization
Go
1
star
58

rust-pico-sys

Rust
1
star
59

rust-phantom

pub struct Phantom<T>;
Rust
1
star
60

rust-async

Rust
1
star
61

underbar

JavaScript
1
star
62

forge-node-modules-repro

reproduction of forge build with node modules contracts
TypeScript
1
star
63

rust-httpparse

Rust
1
star
64

rust-move-acceptor

A variant of the Acceptor trait which moves self in the move_incoming method.
Rust
1
star
65

learning_haskell

Haskell
1
star
66

chatterbox-client-js

JavaScript
1
star
67

chatterbox-server-js

Yet another chat app
JavaScript
1
star
68

rust-rcmut

Rust
1
star
69

rust-rcstring

Rust
1
star
70

line-debug

Lightweight console.log wrapper that logs line number and module name
JavaScript
1
star
71

rust-to

Rust
1
star
72

oath

super-simple promises
JavaScript
1
star
73

fp-animate

JavaScript
1
star
74

iron-try

Rust
1
star
75

beatdots

A fast, greedy algorithm for solving the popular game of dots.
Python
1
star
76

rust-bare

Basic rust template for creating new modules.
Rust
1
star
77

rust-load

Rust
1
star
78

rust-rwlock2

Rust
1
star
79

express-skeleton

express server skeleton
JavaScript
1
star
80

rust-forever

Never-Dropping Data
Rust
1
star
81

rust-poison

Rust
1
star
82

vim-dotfiles

Shell
1
star
83

rust-reduce

Rust
1
star
84

node-to-log

Quickly redirect console.log and console.error to files.
JavaScript
1
star
85

rust-simd

Rust
1
star
86

rust-downcast

Rust
1
star
87

n-queens.js

A blazingly-fast, bit-shifting, backtracking, n-queens algorithm in javascript.
JavaScript
1
star
88

rust-chunks

Rust
1
star
89

Sort-Tester

Fast, comprehensive sort tester
Python
1
star
90

generator-node-module

generate node modules!
JavaScript
1
star
91

go-file-dupes

Get all the duplicate files in a directory, efficiently.
Go
1
star
92

rust-variance

Rust
1
star