Intro to TDD
Why is this important?
This workshop is important because:
- Automated testing can save tons of time and money!
- Test Driven Development guaruntees excellent test coverage
- TDD is a great way for Jr. Devs to contribute a lot of code!
What are the objectives?
After this workshop, developers will be able to:
- Explain what TDD testing is and why people use it
- Write code to pass tests
What is TDD
Test driven development is when tests are written before the code it tests.
It basically works like this:
- A senior or lead dev writes a test suite for a set of User Stories.
- These get handed off to a more junior dev, who write the simplest possible code to get the tests passing.
- They then go back and refactor the code so it is more DRY, readable, maintainable, etc.
"Knowing" TDD means different things.
- It means if you are given a set of tests, you are able to use those tests and write your code to pass the tests
- It means you are able to write the tests before the code.
We are going to give you a basic idea of how the latter works, but as I mentioned this is something that a more senior dev would normally do. It is hard to picture what the code to pass the test should look like in order to write a test for code that doesn't yet exist!
Pros and Cons of TDD
- Guarunteed test coverage
- Good way for Junior Devs to get a lot done
- It's fun to write code to pass tests!
- You need a solid, experienced dev to write the tests! Which is expensive!
- It takes a significant up front investment of time and money.
- If User Stories change, you have to change your tests and the code!
How to get the most out of TDD
There is a lot of discussion on this topic, and many smart people with legitimate opinions, and they don't all agree. I will share two popular patterns for TDD.
I have read of one pattern for TDD that I like the best, and seems to be pretty popular, and works great if you don't have a highly experienced Test Developer:
- Write a E2E test for the feature you want (i.e. User Sign Up)
- Write the code that passes that test.
- Refactor that code
- When that is passing, if there are any stand alone functions that would be a good candidate for a Unit test, write that after.
- If you want, write an integration test.
This is a great deal easier than writing Unit or Integration tests first, because you don't have to make too many low level desicions about design while writing your tests.
This an work well if you do have an experienced tester. It is more difficult, but can result in more comprehensive test coverage:
- Write E2E and integration tests for a feature
- Write the code that passes those tests
- Add Unit tests where appropriate.
There are several more ways of doing things, as long as some tests are written before production code, it's TDD!
We aren't going to go through the process of writing tests, we did that in our E2E and Unit testing lessons! But, if you want practice coding from tests, check out this great lab to create a Express Todo app with TDD! The tests are written in Mocha, but you run the tests the same way, using
npm run test.
If and when you want to work on writing tests first, I suggest pattern one from above, and doing a practice run on an easy project, like an Express Todo API. Happy coding!