DEV Community

Kishan B
Kishan B

Posted on • Updated on • Originally published at

Used TDD approach for a Hello World rust cli app

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.

How did i do it?

  • I created a project using cargo new hello-world-tdd
  • I added a dependency assert_cmd which lets us test out outputs of any binary program
  • Created a file tests/ and I wrote the following test
use assert_cmd::Command;

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");
Enter fullscreen mode Exit fullscreen mode
  • Run the test using command cargo test
  • The above test will fail saying the following, this happens because the cargo new had generated a src/ file which printed out "Hello world!"
running 1 test
test test_cli_app_should_print_hello_tdd_world ... FAILED


---- 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!
Enter fullscreen mode Exit fullscreen mode
  • Now that we have a failing test(RED), the next job is to make it green by modifying the contents of the println statement.
  • Run cargo test again, The test now passes (Green)

The code for this is available at


Latest comments (0)