For the first time ever in my life i wrote hello world application using TDD (Test driven development). Don’t get me wrong here, i have practiced TDD before for larger applications but never have i done it for an hello world app.
Write an CLI application that prints out “Hello TDD world!” in STDOUT.
cargo new hello-tdd-world
assert_cmd
which lets us test out outputs of any binary programtests/cli.rs
and I wrote the following testuse assert_cmd::Command;
#[test]
fn test_cli_app_should_print_hello_tdd_world() {
let mut cmd = Command::cargo_bin("hello-world-tdd").unwrap();
cmd.assert().success().stdout("Hello TDD world!\n");
}
cargo test
src/main.rs
file which printed out “Hello world!”running 1 test
test test_cli_app_should_print_hello_tdd_world ... FAILED
failures:
---- test_cli_app_should_print_hello_tdd_world stdout ----
thread 'test_cli_app_should_print_hello_tdd_world' panicked at 'Unexpected stdout, failed diff original var
├── original: Hello TDD world!
├── diff:
│ --- orig
│ +++ var
│ @@ -1 +1 @@
│ -Hello TDD world!
│ +Hello world!
└── var as str: Hello world!
cargo test
again, The test now passes (Green)The code for this is available at https://github.com/kishaningithub/hello-world-tdd