Skip to content Skip to sidebar Skip to footer

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 a

Solution 1:

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. ;)

Solution 2:

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.

Solution 3:

A unit test is a test of a small portion of the code that you write. You test it in isolation from the rest of the system in order to ensure that that portion of your code works.

It is easier to test a lot of small portions of code, ensure that they work, and then test them working together, rather than testing the system as a whole.

It is important to distinguish between unit testing and automated unit testing. You should always unit test your code. However, automating your unit tests is a whole other subject.

Solution 4:

In addition to what has already be answered, having tests cases covering your code allows you to proceed to Refactoring (improving your code design without modifying its observable behavior).

Unit test frameworks for PHP comprise PHPUnit and SimpleTest that have been compared on StackOverflow.

Solution 5:

Most of the php I've seen is almost impossible to do unit testing on.

Wikipedia has an article on unit testing if you are interested.

Post a Comment for "What Are Unit Tests And Why Should I Care?"