单元测试中什么是正向测试和负向测试

发布于 2024-12-15 18:32:59 字数 93 浏览 0 评论 0原文

我对 TDD 还很陌生。我看到一些文档提到了阳性测试、阴性测试、边界测试等。有人能告诉我阳性测试和阴性测试之间的区别吗?有没有关于不同类型测试的参考资料? (我不是在找书)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

并安 2024-12-22 18:32:59

积极测试 - 通过提供有效的信息来测试系统
数据。

负面测试 - 通过提供无效的内容来测试系统
数据。

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

负测试提高了应用程序的测试覆盖率。结合使用否定和肯定测试方法,您可以使用任何可能的输入数据(有效和无效)来测试您的应用程序,并可以帮助您使您的应用程序更加稳定和可靠。

请参阅此词汇表了解不同类型的测试

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

自此以后,行同陌路 2024-12-22 18:32:59

正与负测试


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

一些快速示例将帮助您更清楚地理解差异。


示例

候选函数:

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

正测试用例-作为该函数需要一个文件路径,正测试用例将包含所有可能的有效文件路径:

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
}

负测试用例-任何可以发送到函数且无效的内容都将处于负测试用例中:

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
}

单元测试还可以作为函数的实时文档。因此,负面测试用例应该仅包含预期的异常条件,而不是向函数抛出所有可能的参数。

因此,不需要测试这个 -

不必要的阴性测试

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

∞梦里开花 2024-12-22 18:32:59

就单元测试而言(这是 TDD 的重点),概念可以简单地描述如下:

  • 正向测试检查函数/方法的行为是否符合其预期输入的预期。
  • 否定测试检查函数/方法在错误输入的情况下是否按预期运行。 (理想情况下,您应该有足够的负面测试来涵盖“坏”的所有可能定义”)请参阅此问题了解更多信息。

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.
昨迟人 2024-12-22 18:32:59

负面测试检查系统是否没有做它不应该做的事情。
示例:如果只有经理可以批准购买新笔记本电脑的请求,则负面测试表明“常规”用户无法批准该请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文