Testing Basics

Testing Basics

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

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:

  1. Write your tests for a specific feature or function you're going to add
  2. Watch the tests fail (since you haven't written the code yet)
  3. Add some code for the new feature
  4. 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.)

Tools

Depending on the language and framework you're testing, there is a plentitude of testing tools available out there. Since this is a JavaScript class covering the MEAN stack, we'll only be focusing on those ones.

Unit testing

For unit testing, there are 2 components to understand - the framework and the runner. The most popular frameworks for JavaScript testing are Jasmine, Mocha, and QUnit. We'll be using Jasmine. You could fully test your JavaScript using just the Jasmine framework, but it is very common to also use a test runner -- specifically the test runner Karma -- for running your tests. Karma makes it much easier to quickly run your tests, try out your code on multiple browsers, and see all the results from the command line without having to do additional work to open up an HTML file in each browser separately. It's also very fast at running tests.

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.


Conclusion

Now that you have an understanding of the basic ideas behind testing your applications, head to the testing JavaScript with Jasmine post to get started with testing your applications!