简体   繁体   English

如何为基于TDD的方法编写否定测试?

[英]How to write a negative test for the method based on TDD?

I have following code and wrote two tests for the positive and negative conditions of it. 我有以下代码,并为它的正面和负面条件编写了两个测试。 But based on TDD I should write a negative test for the whole code as well how should I write that? 但是基于TDD,我应该为整个代码编写一个否定测试,以及应该如何编写?

"There must be at least two unit test cases for each requirement: one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative." “每个需求必须至少有两个单元测试用例:一个正向测试和一个负向测试。如果一个需求具有子需求,则每个子需求必须至少具有两个正负测试用例。”

Code

public class A {

    boolean myOuput(int a) {
        System.err.println(a);

        if(a == 5)

            return true;
        else
            return false;
    }

Tests 测验

public class ATest {
    static A a;
    public ATest() {
    }

    @BeforeClass
    public static void setUpClass() {
        a = new A();
    }

    @AfterClass
    public static void tearDownClass() {
        System.err.println("tearDown class");
    }

    @Before
    public void setUp() {
        System.err.println("setUp");
    }

    @After
    public void tearDown() {
        System.err.println("tearDown");
    }


    @Test
    public void testOutputNotFive(){

        assertEquals(false,a.myOuput(0));
    }

    @Test
    public void testOutputForFive(){

        assertEquals(true,a.myOuput(5));
    }

}

I'm not sure where you are quoting from, but I'm doing TDD since a few years and would write these two tests: 我不确定您在哪里引用,但是由于几年了我一直在做TDD,所以会编写以下两个测试:

shouldReturnTrueIfInputIsFive() {...}

shouldReturnFalseIfInputIsNotFive() {...}

This covers 100% of your code - which is all you want :) 这涵盖了您的代码的100%-这就是您想要的:)

EDIT : As a side note - if you do TDD, you want tests to be as descriptive as possible (they actually replace the documentation). 编辑 :作为旁注-如果您进行TDD测试,则希望测试尽可能具有描述性(它们实际上代替了文档)。 Having a clear naming pattern like should - what - when is very helpful, because if something like testOutputNotFive gives no indication about the desired outcome... 有一个清晰的命名模式- 应该 - 什么 - 什么 时候很有帮助,因为如果像testOutputNotFive这样的testOutputNotFive并没有表明期望的结果...

Your question shows a very simplistic example. 您的问题显示了一个非常简单的示例。 In testing we often find errors occur around boundary conditions. 在测试中,我们经常发现边界条件周围会发生错误。 On either side of a boundary condition you tend to have an equivalence partition. 在边界条件的任一侧,您都倾向于具有等价分区。 ie a range of values that return the same result. 即返回相同结果的一系列值。 For example, an insurance policy may have a restriction on a persons age. 例如,保险单可能对年龄有限制。 The person must be 18-65 years old to take out the policy. 此人必须年满18-65岁才能采取这项政策。 In this example 18 and 65 are boundaries. 在此示例中,18和65是边界。 You should write a test for 18 and 65. You should also write a test for 17 ie 1 less that the lower boundary (all values lower than 17 are on the same equivalence partition that starts at 17). 您应该为18和65编写一个测试。您还应该为17编写一个测试,即比下边界小1(所有低于17的值都在从17开始的同一等效分区上)。 You would need to do the same for 66. Testing 19 and 64 would ensure that you have also covered the inner equivalence partition. 您需要对66做同样的事情。测试19和64将确保您也覆盖了内部等效分区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM