Testing

Summary

Testing is a hugely important and also rather large concept in coding. It is part of every coding language and has been around since coding began. This means we can only briefly touch on some key concepts of testing this sprint.

Time Box

ActivityTime
Learn30 minutes
Reflect10 minutes

Description

Testing is how we know that code works. It tells us our code is reliable, that it isn't going to break and is doing what we want it to. We will often say that code that isn't tested isn't complete yet.

During Foundations, most of the testing you will be doing will be manual testing, running the code and seeing what results or using console.log to check what is happening. But not all testing is manual testing. Instead, we can write tests in our code that say, when I tell this piece of code to run, this is what I expect to happen. Essentially, we write more code to test the code we wrote!

When we write tests, we don't just give ourselves confidence that the code we are writing right now works - it gives us the confidence to change our code in the future. Suppose we make some changes to the codebase, and our test run still passes. In that case, we can feel pretty sure we didn't accidentally introduce any bugs (provided our test coverage is comprehensive).

For example: If we wanted to write a function that would greet someone when given their name, the test we would write might look something like:

function testGreeting() {
const expected = 'Kia ora Cam!'
const actual = greeting('Cam')
if (actual !== expected) {
console.log("It's broken..")
} else {
console.log('It works!')
}
}

A function that passes this test would then be:

function greeting(name) {
return 'Kia ora ' + name + '!'
}

Two concepts within testing that we will see a lot of are Unit Testing and Test Driven Development. These help us write tests to ensure our code is reliable, but for now, it is enough to get familiar with the concepts, as we won't be asking you to write your own tests until Bootcamp.

Unit Testing

Unit testing is a particular type of testing. As our code is divided into small individual functions that make up a larger programme, we can test these small units of code to ensure each part is doing what we expect.

Unit testing doesn't care if our project works as expected but instead makes sure each of its functions does what we need them to. As we can rely on the small pieces of code, we can build our larger pieces on top of them, knowing they are working correctly.

There are other types of testing, such as integration testing, our end-to-end testing, which test the plumbing of our application - that our functions are working together as expected. However, these can be more complex to write and time-consuming to run, so we try to cover as much of our codebase as possible with unit tests.

This testing method means that when we change something in our code, we do not need to manually test our large project to find out if we broke anything, but the tests will also tell us where in our code this has happened if so.

Test Driven Development (TDD)

TDD is a particular way of doing code development as a whole. It involves starting with the big picture and writing your tests before you write your code.

This way of coding means you have to work out what you intend for the code to do and what smaller things need to happen to achieve this before jumping into writing it. Tests are then written that match this intent for your project.

You don't need to have written tests for your whole application before you start building, but in a TDD approach, you would write the test/s for the next feature or function before implementing it.

Once the test is written, you can write the code that makes the test pass. Because of this, TDD forgoes writing "nice" code - so long as it works to meet the test's needs, you're on the right track. This also means we can try many different methods to achieve the same goal as it doesn't matter how we get there, just that we do. Once the code works, we go back and refactor it to make it more concise and readable.

Reflect

In your my-reflections-sprint-4 file, answer the questions under the Testing heading.

Commit and push the changes to GitHub.

Some interesting links: