What is positive test and negative test in unit testing
===============================================================
| Positive Test Case | Negative Test Case |
+==============================+==============================+
| test by valid/expected data | test by invalid data |
+------------------------------+------------------------------+
| check if the function does | check if the function does |
| that it should do | not that it should not do |
+------------------------------+------------------------------+
| examine general behaviors of | examine if the function |
| the function | is fault proof (does not |
| | crush/mis-response in bad |
| | situations) |
===============================+===============================
Some quick examples will help you to understand the difference more clearly.
Example
Candidate Function:
public boolean deleteFile(String filePath) {
// try to delete the file; and
// return true for success, false for failure
}
Positive Test Cases- As this function expects a file path, positive-test-case will comprise with all possible valid file paths:
public void deleteFile_forAbsoluteFilePath_P() {
String filePath = "D:\\Temp\\file.txt";
// create file, call deleteFile(), and check if really deleted
}
public void deleteFile_forRelativeFilePath_P() {
String filePath = "file.txt";
// create file, call deleteFile(), and check if really deleted
}
public void deleteFile_forNonExistingFilePath_P() {
String filePath = "wHSyY#zP_04l.txt";
// call deleteFile(), and check if false is returned
}
public void deleteFile_forSymlinkedFilePath_P() {
String filePath = "D:\\Temp\\symlink\\dir\\file.txt";
// create file, create symlink, delete file, and
// check if really deleted
}
public void deleteFile_forUndeletableFile_P() {
String filePath = "file.bin";
// create file, restrict delete permission, call deleteFile(), and
// check if does not crash and returns false
}
Negative Test Cases- Any thing that can be sent to the function and is not valid, will be in negative-test-case:
public void deleteFile_forAlreadyDeletedFile_N() {
String filePath = "file.bin";
// create file, call deleteFile() twice, and
// for second time check if does not crash and returns false
}
public void deleteFile_forDirectoryPath_N() {
String dirPath = "D:\\Temp\\dir";
// create directory, call deleteFile(), and check if false is returned
}
public void deleteFile_forSymlinkedDirectoryPath_N() {
String symlink = "D:\\Temp\\symlink";
// create symlink, call deleteFile(), and check if false is returned
}
public void deleteFile_forNullString_N() {
String filePath = NULL;
// call deleteFile(), and check if does not crash and returns false
}
public void deleteFile_forCorruptedFilePath_N() {
String filePath = "D:\\Tem¡¿ÿ¿";
// call deleteFile(), and check if does not crash and returns false
}
Unit-test also works as a live documentation to your function. So, instead of throwing every possible argument to the function, negative test case should comprise only with expected exceptional conditions.
Hence, don't need to test this-
Positive Testing - testing the system by providing valid data.
Negative Testing - testing the system by providing invalid data.
For Example, an application contains a textbox and as per the user's Requirements the textbox should accept only Strings.By providing only String as input data to the textbox & to check whether its working properly or not means it is Positive Testing. If giving the input other than String means it is negative Testing..
Negative testing improves the testing coverage of your application. Using the negative and positive testing approaches together allows you to test your applications with any possible input data (both valid and invalid) and can help you make your application more stable and reliable.
Refer this Glossary for different type of tests
In terms of unit testing, (which is the focus of TDD) the concept can be described simply as follows:
- A positive test checks if a function/method behaves as expected with its expected input.
- A negative test checks if a function/method behaves as expected with bad input. (you should have enough negative tests to cover all possible definitions of "bad", ideally") See this question for more information.