Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are unit tests and why should I care?

Okay, I develop web applications in PHP and JavaScript and a lot of times here on Stack Overflow I have seen the word unit test passing by, but nowhere on the website have I been able to found a satisfactory answer to what exactly a unit test is.

So what are unit tests and should I, as a PHP and JavaScript programmer, care, or are they only for 'real' programming languages?

like image 381
Pim Jager Avatar asked Dec 06 '08 19:12

Pim Jager


People also ask

What is unit testing and why is it important?

The main objective of unit testing is to isolate written code to test and determine if it works as intended. Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.

What do you use unit tests for?

Tests written using the unittest module can help you find bugs in your programs, and prevent regressions from occurring as you change your code over time. Teams adhering to test-driven development may find unittest useful to ensure all authored code has a corresponding set of tests.

What is unit testing and its advantages and disadvantages?

Unit testing is a functional testing method that those in the field have different opinions about. Some think it's necessary and useful while others don't like to use it unless they really have to. These two groups each have valid arguments as there are both advantages and disadvantages to unit testing.


2 Answers

Unit tests are for any code you wish to maintain.

In a nutshell, the idea is to write many small tests, each of which can be run in isolation, and test the smallest possible part of your codebase (often individual classes or individual functions). If I give this function the input it expects, does it return the output I expect? If it does, that means the rest of the application can pretty much assume it works. And if it doesn't, I'd rather catch the error in a small, simple, isolated unit test function than trying to trace it through the entirety of my application.

Of course, it also requires you to be fairly disciplined in how you write your code, both because it has to be possible to isolate individual functions or classes to test them, and because, well, the tests don't write themselves. You have to do that. ;)

Given the quality of most of the PHP code I've seen, I'd say unit testing definitely has its place in the PHP community. More so than in almost any other language, in fact. ;)

like image 50
jalf Avatar answered Oct 07 '22 01:10

jalf


Unit tests are automated tests that test whether a given piece of code does what you expect it to do under a given set of circumstances. Good unit tests test small pieces of functionality, often at the level of individual functions.

Unit tests are usually structured such that you set up some state, run the function or method you want to test, and then assert either the output of that function, or a change in other state as a result of that function. Most unit testing frameworks have utilities for supporting and structuring this, such as:

  • Test cases: Ways to group similar tests together, often that test the same file or section of code.
  • Setup/teardown functions: functions that are called before each test that setup and cleanup state that is the same across multiple tests.
  • Assertion functions: functions that allow you to assert that specific things are true, such as a value is equal to another expected value, that a value is true/false, etc. When they fail, they show you what the expected and actual values were.
  • A test running application: An application that runs all of your tests for you one after another, showing you whether each succeeded or failed, and if it failed, what specific assertions failed, and what the actual value was as opposed to the expected value.

Unit tests are useful for a number of reasons:

  • They ensure your code works, as you're writing it, without having to test it manually. As you're writing a function, you can test your assumptions about how it works, catching bugs right away.
  • They keep working code working, even as you or someone else changes it. Once a function is written and fully unit tested (ie, all of the things it should do have associated unit tests that verify they actually do that), then if you or anyone else breaks any of that functionality, you'll know right away when you run the unit test, and can easily rectify it.
  • They help you debug. Rather than having to look through large volumes of code when trying to diagnose a bug observed from manually testing your application, unit tests allow you to focus on smaller pieces of functionality, reducing the amount of complexity you need to think about.
  • They document your code in a way that stays updated as the code changes. Design documents and comments are easy to forget to update. Unit tests, on the other hand, break when they're not updated. If clearly written, they show what code actually does in a specific situation, and can prove that to you.

Unit tests are useful on any code that's larger than a few lines, regardless of language. I've definitely unit tested php code, even for relatively small projects, and have gotten immediate rewards from it.

like image 29
Scotty Allen Avatar answered Oct 07 '22 00:10

Scotty Allen