What You Need to Know About Unit Testing

What You Need To Know About Unit Testing

Notes

What Are Unit Test?


A unit test is a way to break up your code into small blocks that are manageable and testable to ensure your code functions as expected.  By focusing on specific behaviors within your code you are able to ensure that the right inputs and outputs are processed as intended. Coupled with an automation process and framework, unit tests can be run continuously limiting human error and providing instant feedback to the developer during coding sessions. 

What makes a good unit test


Readable


Think of the unit tests as a blueprint for your application or the documentation itself. It should be easy to read, follow and understand with very little external documentation or explanation from a developer. The best way to maintain that is by keeping it simple, group your unit test into logical sections and within those sections limit your tests to one feature or behavior. 

Predictable


With every iteration of your tests, you want to make sure that it runs as expected every single time. Even if you have randomized data such as strings, ids or numbers; The input data shouldn’t alter the test; provided it fits within the expected behavior or requirements. Remember to keep it simple don't overcomplicate your code or unit test by trying to do too much in one area. Instead, opt to create more unit tests to better focus on the behavior you trying to establish. 

Speed and Scaleability


With larger projects, you can get into the 10s thousands of unit tests, testing times can increase relatively quick. At two seconds per test and 10 thousand tests (10K * 2sec = 5.5 hours), it would take roughly 5.5 hours to run the complete unit test. There are ways to increase the speed that your unit test run during your build process, increasing your server capacities and running tests in parallel are a few options.  However, your developers still need to run those tests locally. The best way to keep your unit test time down is to limit the complexity as well as mock any external resources that the unit test requires.

A good example would be connecting to a MongoDB or AWS  Elasticsearch cluster, if you create a new connection with every unit test, your test will have to pause until a connection is made. Let’s say that it takes 3 seconds per connection and you have 10 connections, you will have added 30 seconds in running time for connecting to the cluster. A better option would be to cache one of those connections and reuse them. This will then take you to 3 seconds per unit test Suite( which is just a collection of unit tests). 

The best solution would be to mock the connection instead.  That way you never actually connect to the database instead, you use a mock API that mimics the calls that you would make to the database. Leave the actual data Integrity testing to integration and end-to-end tests instead.

Know your tools and frameworks


​It's important that you understand the inner workings of whatever framework or tools you are using for unit testing. You should be able to answer the following questions.

  1. What's the best way to solve specific problems?
  2. What's the fastest and most efficient way to run your unit test?
  3. Do your tools allow for cloud service integration?
  4. Are there any special settings that will speed up your unit testing.
  5. Is the framework still in active development, if not are you willing to maintain it if it is open source?
  6. Is there a community you can ask questions?

Take some time to read the documentation and understand your testing framework and tools so that you can write efficient unit tests that are up-to-date with your Frameworks standards.

 

You May Also Like
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email

Signup NOW and Never be late again

Get all the latest content regularly and stay up to date with all the latest tools, techniques and industry news.