18 – The journey of 1000 miles starts with a single step …… Unit Testing

Date: 1/12/2020

Guests: none
Welcome and greetings
Recap of last episode

  • In the last episode, we discussed Test Automation and how it can be used to accelerate your pipelines. We also discussed some of the methods of automating tests and some products that can help you with your automation.

Summary of this episode

  • In this episode, I’m going to discuss the lowest level of testing possible by a developer, and that is Unit Testing. I’m going to give a brief description of what it is, what value or benefits that it can bring to the table, when to start it, and some examples of how it can be implemented.

What’s in it for you?

  • After listening to this episode, you should have a better understanding of what unit testing is, how it is different from other types of testing and how it can be used to speed up some of your development efforts.

Episode Content

  • What is it?
    • Unit Testing is when a developer writes tests to validate the smallest piece of code possible.
      • Methods, functions, procedure calls, etc.
    • A Unit is a self contained piece of code that typically has a small number of inputs and outputs.
    • A Unit Test can have constructor and destructor methods to ensure that any needed data or structures are created before the test is run and cleaned up once the test is done.
  • What benefits does it provide?
    • Testing smaller pieces of code is faster than testing the whole program.
    • Tests are simpler since they cover a smaller set of code.
    • Defects found in unit testing are cheaper to fix since the change should be smaller.
    • Debugging defects found in unit testing is faster due to the smaller set of code to review.
  • When should I start using it?
    • When you write the first method, function, procedure call.
    • You should write your unit test cases in tandem with your program code.
    • The earlier the better.
    • It is one of the predecessors of Test Driven Development.
  • What can it be used with?
    • Any of the modern programming languages should have their respective unit testing framework. Java has Junit, c variants have xunit, python has pytest…..you get the picture.
  • How to use it? A real life example for Java (kind of)
    • method call

public int addNumber(int x, int y) {return x+ y;}

  • Unit test

public void testAddNumber() {double result = addNumber(2,3);assertTrue(result == 5)}

  • Unit Tests are grouped into TestSuites
  • Test Suites are executed by TestRunners
  • Test Suites are classes that contain a grouping of individual Unit Tests that relate to a specific Java class that you are developing.
  • TestRunners are classes that are written to consume the Test Suites, execute any test cases found in the TestSuite class, and raise the result for the entire run of the TestSuite.
  • There are several different types of frameworks, Junix, xunit, pytest, mockito.
    • You should choose the one that is appropriate for the program that you are developing.
  • Each framework while similar, has its own syntax and procedures.
  • How to get the most out of your Unit Testing?
    • Pick the framework that is designed for the language that you are working with.
    • Keep the test unit as small as possible (single method or function).
    • Run your Unit Tests often – faster feedback is better.
    • Update your Unit Test Cases as changes are made to your codebase. Don’t wait until the end to write new Unit Tests or update existing ones.

Recap of this episode

  • In this episode, I discussed unit testing, how it benefits developers and development efforts, and examples of how it can be implemented.

Like me Like my podcast – share, like, thumbs-up, review, subscribe
Next Episode: Family Feud – Component Integration Testing