Testing Flashcards
First Tests (bottom of pyramid)
Unit Tests: The smallest unit of testing - used to test the smallest pieces of your application in isolation to ensure each piece works before you attempt to put those pieces together. Each unit test should focus on testing one thing. These are generally the fastest tests to write and run.
Second Tests (middle of pyramid)
Integration Tests: Once you have your unit tests in place you know each piece works in isolation - but what about when those pieces interact with each other? Integration tests are the next level up, they will test the interactions between two pieces of your application. Integration tests will ensure the units you’ve written work coherently together.
Third Tests (top of pyramid)
End-to-End (E2E) Tests: End-to-end tests are the highest level of testing - these will test the whole of your application. End-to-end tests are the closest automated tests come to testing the an actual user experience of your application. These are generally the slowest tests to write and run.
Why TDD?
Writing tests before code ensures that the code written works.
Code written to pass specs is guaranteed to be testable.
Code with pre-written tests easily allows other developers to add and test new code while ensuring nothing else breaks along the way.
Only required code is written.
In the face of having to write tests for every piece of added functionality TDD can help reduce bloated un-needed functionality.
TDD and YAGNI (“you ain’t gonna need it”) go hand in hand!
TDD helps enforce code modularity.
A TDD developer is forced to think about their application in small, testable chunks - this ensures the developer will write each chunk to be modular and capable of individual testing.
Better understanding of what the code should be doing.
Writing tests for a piece of code ensures that the developer writing that code knows what the piece of code is trying to achieve.
TDD Workflow
Red: Write the tests and watch them fail (a failing test is red). It’s important to ensure the tests initially fail so that you don’t have false positives.
Green: Write the minimum amount of code to ensure the tests pass (a passing test will be green).
Refactor: Refactor the code you just wrote. Your job is not over when the tests pass! One of the most important things you do as a software developer is to ensure the code you write is easy to maintain and read.