Skip to content

Commit f8452fb

Browse files
committed
Added property testing
1 parent 22fa90b commit f8452fb

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ authors = ["davidf <[email protected]>"]
55

66
[lib]
77

8-
[dependencies]
8+
[dependencies]
9+
10+
[dev-dependencies]
11+
proptest = "0.7.1"

slides/tdd.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ revealOptions:
77
# Testing in Rust
88

99
* Language Feature
10-
* Specialized Libraries
11-
* e.g. for Property Testing
10+
* Rust Testing Libraries
11+
* e.g. proptest, netsim
1212
* Language-agnostic tools
1313
* e.g. Jepsen, TLA+
1414

@@ -82,4 +82,46 @@ extern crate test;
8282

8383
* Based on Python's "Hypothesis" module
8484
* Which in turn is based on Haskell's "QuickCheck"
85-
* By convention appended to the same file to be tested
85+
* Randomly checks over an input range
86+
* Automatically reduces to a *minimal* test case
87+
* Writes seed for random number generation in case of failure
88+
* Makes random failures repeatable
89+
* Combine with traditional Unit Testing for edge cases
90+
91+
----
92+
### Usage
93+
94+
* Import proptest crate with using_macros and test attributes
95+
```
96+
#[cfg(test)]
97+
#[macro_use]
98+
extern crate proptest;
99+
```
100+
* Then use the proptest! macro to define tests
101+
102+
----
103+
104+
### Example
105+
106+
```
107+
proptest! {
108+
#[test]
109+
fn a_proptest(a in (0i32..100),
110+
b in (0i32..100)) {
111+
assert!(a * b <= 10000);
112+
}
113+
}
114+
```
115+
116+
----
117+
118+
### String Patterns
119+
120+
* proptest supports more advanced range patterns
121+
* patterns extensible as "Strategies"
122+
```
123+
#[test]
124+
fn parses_all_valid_dates(s in "[0-9]{4}-[0-9]{2}-[0-9]{2}") {
125+
parse_date(s).unwrap();
126+
}
127+
```

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![feature(test)]
22
extern crate test;
33

4+
#[cfg(test)]
5+
#[macro_use]
6+
extern crate proptest;
7+
48
mod child_module;
59

610
pub fn a_public_function() -> i32 {
@@ -27,4 +31,12 @@ mod tests {
2731
fn a_benchmark(b: &mut Bencher) {
2832
b.iter(|| a_public_function());
2933
}
34+
35+
proptest! {
36+
#[test]
37+
fn a_proptest(a in (0i32..100),
38+
b in (0i32..100)) {
39+
assert!(a * b <= 10000);
40+
}
41+
}
3042
}

0 commit comments

Comments
 (0)