23

I'm pretty new to TDD. I see some docs says about positive test, negative test, boundary test etc. Can any one tell me the difference between a positive test and negative test? Is there any reference out there that says about the different types of tests? (I'm not looking for books)

VJAI
  • 29,899
  • 20
  • 93
  • 155

4 Answers4

31

Positive Testing - testing the system by giving the valid data.

Negative Testing - testing the system by giving the 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

Rohan Patil
  • 2,270
  • 1
  • 13
  • 22
13

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.
Community
  • 1
  • 1
mpontillo
  • 12,623
  • 7
  • 51
  • 87
9

Positive Vs Negative Test


===============================================================
|      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-

Unnecessary Negative Test

Minhas Kamal
  • 13,962
  • 5
  • 47
  • 55
2

Negative testing checks that the system does not do what it shouldn't. Example: If only a manager can approve a request for a new laptop, negative testing shows that a "regular" user cannot approve that request.

AJM
  • 21
  • 1