The aim of this post is to help you gain the basic understanding of the principles and reasons behind testing, as well as to learn some basic testing terminology.
What does "testing" mean in software development?
Have you ever written a new feature into your code, and then had to spend the next 10, 20, 60 minutes running through each of the existing features that were already working just to make sure your new code additions didn't mess with anything from the past? This manual testing is both inefficient and error-prone. Automated tests help us perform these tests programmatically, so all we have to do is run a command after we finish adding new code and it will check all the tests we've written, making sure nothing is broken in past or new code.
Writing tests diligently is akin to keeping up with your car maintenance - it can seem inconvenient when you're first starting, but it can save you a LOT of headache, time, and even money (depending on the app you're making) in the future.
Types of tests
In software development, there are a few different types of tests you can write:
Unit tests are a way to test single, individual pieces of code, oftentimes the functionality of a single function or method.
End-to-end (e2e) tests
End-to-end testing is when you write tests that make sure your entire application is working from beginning to end. It often follows the typical workflow that a human may take through your app and makes sure the end result is as expected.
Test Driven Development (TDD)
The idea behind TDD is that you write your tests before even implementing the new feature. This way you set a benchmark for when the feature is complete (once all the tests you wrote for that feature pass), and you ensure that every feature in your application is tested and ready for deployment. The steps to TDD are:
- Write your tests for a specific feature or function you're going to add
- Watch the tests fail (since you haven't written the code yet)
- Add some code for the new feature
- Run the tests again. If something still fails, begin step 3 again, and iterate until all tests pass. This is called the "Red, Green, Refactor" cycle. (Red = test failure, green = test passes, refactor = change the code to be more maintainable and to follow best practices, etc.)
End to end testing
To do e2e testing we'll be using a framework called Protractor, which was created by the AngularJS team. This allows you to test behaviors that your users will frequently be following from one end of your application to another, such as signing up as a new user, logging in, and then logging back out.