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?
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.
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.
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.
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. ;)
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:
Unit tests are useful for a number of reasons:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With