Testing is an important part of programming that ensures code quality and makes life easier in the long run.

Intro

If you are a complete beginner and have next to zero programming knowledge, then this section might not make as much sense. It should make more sense after making a few small projects and understanding how coding works in general. We won’t actually go deep into testing in these series of articles, so you can skim through it if you want, but this is definitely an important topic to know about somewhere down the line. 

In this article, we will briefly discuss the importance of testing code. When writing code, it is good practice to write tests for the code as well. This way, if you make any changes to the application in the future, you can run the tests to see if they still work as intended without having to manually test every function again to see if they were affected. We will go through a few testing topics below just to get an understanding of it.

Unit Tests

Unit tests are essentially pieces of code that test other code. For example, if we have the below function in Java: 

int x = 1;

int addYToX(int y) 
{
 return x + y;
}

A test for this function could look like this: 

public void testAddYToX() 
{
  int result = addYToX(2);
  assertEquals(3, result);
}

Note: 

– You can ignore the “public void” part for now if you are not familiar with it. The main point of the first function is to take a variable ‘y’ and add it to x, which has an initial value of 1. The test below then tests it by giving the addYtoX a value of 2. The “assertEquals” function then checks to verify if adding 2 to x that the function will return 3. 

Test Driven development (TDD)

This is a style of development that is very popular for companies today. TDD means that you are writing your tests first before writing the code. This makes it more likely for developers to cover more of their code with tests. 

In some cases, writing tests can also help a person write better code. This is because, writing testable code usually involves breaking a function down into smaller/testable pieces, which prevents functions from getting too convoluted. Making functions simpler, more readable, and shorter is usually a good way to prevent headaches in the future. 

Additionally, writing tests also help you think about covering edge cases and how the functions should handle unexpected input. This can make the code more robust and less likely to error out with unusual inputs. 

Behaviour Driven Development (BDD)

Behaviour Driven Development just is a way to write or implement the tests. In the above example, we tested whether adding 2 to x will equal 3 if x starts at 1. This may work if x is always expect to be 1, but if x starts at 2, then we would have to rewrite the test to to check if adding 2 to x equals 4. 

To help prevent the need of rewriting this, we can test the behaviour the function instead of hard-coding things in. In this case, the behaviour of the function “addYToX” is to add the variable y to the variable x, so we can specify that in the test instead: 

public void testAddYToX() 
{
  int result = addYToX(2);
  assertEquals(x + y, result);
}

This way instead of changing and specifying the expected result each time, the test is saying that the expected result should be x + y. 

For more details on testing, please feel free to refer to this article: 

http://softwaretestingfundamentals.com/unit-testing/

Most programming languages have a unit testing framework to help you get started with implementing tests for the project. You can learn more about the basics of testing from here: 

For Java: https://www.tutorialspoint.com/junit/

For Ruby: https://www.tutorialspoint.com/rspec/

Navigation

The next article can be found here. Previous article is here.