什么是单元测试中的阳性测试和阴性测试

VJA*_*JAI 21 tdd unit-testing

我对TDD很新.我看到一些文档说关于阳性测试,阴性测试,边界测试等.任何人都可以告诉我阳性测试和阴性测试之间的区别吗?是否有任何关于不同类型测试的参考?(我不是在找书)

Roh*_*til 25

正面测试 - 通过提供有效数据来测试系统.

负面测试 - 通过提供无效数据来测试系统.

例如,一个应用程序包含一个文本框,根据用户的要求,文本框只应接受字符串.仅提供字符串作为文本框的输入数据,并检查其是否正常工作意味着它是肯定测试.如果给出除String之外的输入意味着它是否为负测试..

负面测试可以改善应用程序的测试覆盖率.同时使用负面和正面测试方法,您可以使用任何可能的输入数据(有效和无效)测试应用程序,并可以帮助您使应用程序更加稳定可靠.

有关不同类型的测试,请参阅此术语表

  • 您链接到的词汇表作为参考,为您的答案提供了完全不同的定义. (2认同)

mpo*_*llo 12

在单元测试方面(这是TDD的重点),概念可以简单描述如下:

  • 阳性测试,如果功能/方法的行为支票作为与它的预期输入的预期.
  • 阴性测试如果一个功能/方法的行为的检查与坏输入预期.(您应该有足够的负面测试来涵盖所有可能的"坏"定义,理想情况下")请参阅此问题以获取更多信息.


Min*_*mal 6

正与负测试


===============================================================
|      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)                  |
===============================+===============================
Run Code Online (Sandbox Code Playgroud)

一些简单的示例将帮助您更清楚地了解差异。


候选功能:

public boolean deleteFile(String filePath) {
    // try to delete the file; and
    // return true for success, false for failure
}
Run Code Online (Sandbox Code Playgroud)

正测试用例-由于此功能需要文件路径,因此正测试用例将包含所有可能的有效文件路径:

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
}
Run Code Online (Sandbox Code Playgroud)

否定测试用例-可以发送到该函数且无效的任何事物都将处于否定测试用例中:

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
}
Run Code Online (Sandbox Code Playgroud)

单元测试还可以作为功能的实时文档。因此,否定测试用例应该只包含预期的例外条件,而不是将所有可能的参数扔给函数。

因此,无需对此进行测试-

不必要的阴性测试