【问题标题】:What is positive test and negative test in unit testing什么是单元测试中的正测试和负测试
【发布时间】:2011-12-31 00:52:45
【问题描述】:

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

【问题讨论】:

    标签: unit-testing tdd


    【解决方案1】:

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

    • 肯定测试检查函数/方法的行为是否与预期的输入一样。
    • 否定测试检查函数/方法在输入错误的情况下是否按预期运行。 (你应该有足够的负面测试来涵盖“坏”的所有可能定义,理想情况下)见this question了解更多信息。

    【讨论】:

      【解决方案2】:

      正面测试 - 通过提供有效的测试系统 数据。

      Negative Testing - 通过提供无效的测试系统 数据。

      例如,一个应用程序包含一个文本框,并且按照 用户的要求文本框应该只接受 Strings.By 仅将 String 作为输入数据提供给 文本框 & 检查其是否正常工作 表示它是正面测试。 如果给出 String 以外的输入意味着它是负数 测试..

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

      请参阅Glossary 了解不同类型的测试

      【讨论】:

      • 您作为参考链接到的词汇表对您的答案给出了完全不同的定义。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:


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

        Positive Test Cases- 由于此函数需要一个文件路径,因此 positive-test-case 将包含所有可能的有效文件路径:

        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- 任何可以发送到函数但无效的东西,都将在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
        }
        

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

        因此,不需要测试这个-

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-21
          • 2010-10-05
          • 1970-01-01
          • 1970-01-01
          • 2018-03-23
          • 2021-09-17
          • 2018-01-18
          相关资源
          最近更新 更多