Efficient TDD in RUST
Thomas WICKHAM
@mackwic
RustFest 2017
Zurich
A bit about me...
Open Source by night
@mackwic
@mackwic
mackwic@gmail.com
PARIS - RABAT - LAUSANNE - SAO PAULO - SYDNEY
Slides
Links
are online
in the notes
T
D
D
ests
riven
evelopment
Why do we test first ?
Why
first
Easier to write
Easier
Test the intent
intent
Helps my
design
Test my understanding
understanding
Executable
specification
I test first because:
- It's easier to write
- I want to test the intent
- It helps me in my design
- It challenges my understanding
- It is an executable specification
ROI of TDD links in appendices
if you need to argue with your boss...
How does it works ?
Red
Green
Refactor
RED
GREEN
REFACTOR
And again...
How to choose the next test ?
A quick example
RED: To make a function, call it
#[test]
fn it_exists() {
Divisor::divide()
}
error[E0599]: no function or associated item named `divide` found for type `Divisor` in the current scope
GREEN: create the function
impl Divisor {
fn divide() {}
}
running 1 test test it_exists ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
REFACTOR: nothing to do
RED: put a simple error case
#[test]
fn it_err_when_zero_is_given() {
// arrange
// act
let res = Divisor::divide(12, 0); // division by 0
// assert
assert!(res.is_err())
}
GREEN: adapt the signature
impl Divisor {
fn divide(numerator: u32, denominator: u32)
-> Result<(), ()> {
Err(())
}
}
REFACTOR: nothing to do
Rince and Repeat
-
The next test: a muscle to train
-
Hard because it forces to slow down
-
You don't need anything. Start now
General tips and remarks
There is no NULL and it helps us
error_chain is really nice
Where do I put my tests ?
Unit tests in `_test` modules
Where do I put my tests ?
Integration tests in `tests/` directory
Where do I put my tests ?
Functional tests in `examples/` directory
and Documentation exemples
Group tests of one method
in a module
mod task {
pub use parser::task::*;
mod resolve_include {
pub use super::*;
#[test]
fn it_exists() {...}
#[test]
fn it_returns_a_full_path() {...}
#[test]
fn include_is_an_error_when_reading_from_stdin() {...}
}
mod has_name {...}
mod is_templated {...}
}
The super neat `pub use super::*`
mod task {
pub use parser::task::*;
mod resolve_include {
pub use super::*;
#[test]
fn it_exists() {...}
#[test]
fn it_returns_a_full_path() {...}
#[test]
fn include_is_an_error_when_reading_from_stdin() {...}
}
mod has_name {...}
mod is_templated {...}
}
Think readability:
Please Do Repeat Yourself in your tests
A failing test should be a complete and explicit bug report
Closing thoughts
If you need a
SetUp/TearDown it's most probably an Integration Test
Want Culture Code translated ?
LAUSANNE:
ch-info@octo.com (or jglorieux@octo.com)
PARIS:
recrutement@octo.com
(or twickham@octo.com)
APPENDICES
ROI of TDD links
-
About the ROI of TDD —https://pdfs.semanticscholar.org/65d9/24ef1162b2f3c24bd15d627302887991b298.pdf
-
http://robertvbinder.com/wp-content/uploads/sites/4/2011/06/Whats-My-Testing-ROI1.pdf
-
Applying TDD by a single developer to legacy codebase over years reduced bugs by 50% — OMG, Test Driven Development Actually Works! (September 2012)
-
New projects built with TDD approach had a smaller bugs to lines of code ratio than non-TDD ones — Realizing quality improvement through test driven development: results and experiences of four industrial teams (February 2008)
-
5 year study with developers inexperienced in TDD showed better software quality using TDD than the industry average — A longitudinal Study of the Use of a Test-Driven Development Practice in Industry (2007)
-
Study on open source projects found that TDD increased code quality 20% — Quantitatively Evaluating Test-Driven Development by Applying Object-Oriented Quality Metrics to Open Source Projects (December 2009)
-
The deep synergies between good design and testability: https://vimeo.com/15007792
-
Great success stories, see REX Service-Public.fr (FR vid: https://youtu.be/eEUfsjYj3rw)
Efficient TDD in Rust
By Thomas Wickham
Efficient TDD in Rust
- 2,981