Test Driven Development Theory

Test Driven Development Theory

Test Driven Development Theory

Throughout these tutorials, you will be guided to use a practice called Test Driven Development (TDD).

Before you can understand what Test Driven Development is, you need to understand what a Unit Test is.


Unit Tests

Units Test refers to an automated process which verifies that some specific aspect of the code does what is expected of it. That is to say, it's code which tests other code.

"Unit" refers to breaking the code to be testing into very small and specific pieces.

Rather than focusing on a calculator application, the code can be broken into logical chunks, such as addition, subtraction, multiplication, and division.

Drilling further into the addition aspect, the code can still be broken down into input of the numbers to be added, the processing, and the output of the sum.

A Unit Test, then, would be a test dedicated to one of those specific aspects of the calculator. For the calculator application, a suite of tests would exist. One specific test within that suite would be to verify that the code which inputs the numbers to be summed for an addition operation works as expected.

Consider you are creating the addition aspect of the calculator application without using TDD. The user inputs two numbers, and sees the results displayed on the page. When you have finished building the page, you will likely test it manually by entering a series of numbers to verify the output is what you expect. You may also test some unexpected use cases (edge cases), such as entering words instead of numbers, to see how the page responds.

If, at any point in the future, you lose confidence that the page is working as intended, you will likely execute more of these types of manual tests.

The goal of testing is automation of such processes. Once you have written code which knows how to use that webpage and verify it works as expected, you can very quickly verify it at any time. Beyond that, it is very common to have a whole suite of tests run every time any code changes. This allows you to move forward with much more confidence that the changes you have made did not have any unintended consequences.


What is Test Driven Development?

Test Driven Development is simply the practice of writing the Units Test prior to writing the code being tested.


Why?

Approaching new code using this process has many benefits.

Using this approach causes a shift in the analysis and design of the product. Writing the test first prompts one to think about the big-picture needs in more thoughtful and complete perspective while delaying digging into the nuts and bolts of how the code works. With this, it is often broken into smaller and more easy to work against needs. You, the developer, has more information and context about where you are going and how the piece you are currently building will fit in.


Running Tests

Laravel comes with a few simple tests ready to run out-of-the-box. You can run them and see the results from your terminal using:

sail artisan test