基于三角形问题通过边界值分析和等价类划分进行黑盒测试

等价类划分

 

 

 

 

 

 

 

 

 

 

 

输入三个整数

 有效等价类

 无效等价类

1、 整数

2、 3个整数

3、 非0整数

4、 正数

 

 

 

 

13、一边为非整数

14、两边为非整数

15、三遍为非整数

16、数字个数小于3

17、数字个数大于3

18、一边为0

19、两边为0

20、三遍为0

21、一边小0

22、两边小于0

23、三边小于0

24、一边大于100

25、两边大于100

26、三边大于100

 

构成不等边三角形

5、 a+b>c

6、 a+c>b

7、 b+c>a

27、a+b<=c

28、b+c<=a

29、c+a<=b

 构成等腰三角形

8、 a=b且满足5

9、 b=c且满足7

10、    c=a且满足6

 

构成等边三角形

 11、a=b=c

 

不构成三角形

 12、不满足5,6,7中的一个

 

 

等价类划分测试用例

 

等价类划分测试用例

测试用例编号

等价类条件

测试用例

预期结果

a

b

c

d

1

1,2,3,4,5,6,7

2

3

4

 

普通三角形

2

1,2,3,4,5,6,7,8

2

2

3

 

等腰三角形

3

1,2,3,4,5,6,7,9

2

3

2

 

等腰三角形

4

1,2,3,4,5,6,7,10

3

2

2

 

等腰三角形

5

1,2,3,4,5,6,7,11

2

2

2

 

等边三角形

6

1,2,3,4,12

1

2

3

 

不构成三角形

7

1,2,3,4,5,6,7,13

2.1

3

4

 

不构成三角形(未测试)

8

1,2,3,4,5,6,8,14

2.1

2.1

3

 

不构成三角形(未测试)

9

1,2,3,4,5,6,9,15

2.1

2.1

2.1

 

不构成三角形(未测试)

10

1,3,4,,16

2

3

null

 

不构成三角形(未测试)

11

1,2,3,17

2

3

4

3

不构成三角形(未测试)

12

1,2,18

0

2

3

 

抛出异常

13

1,2,19

0

0

2

 

抛出异常

14

1,2,20

0

0

0

 

抛出异常

15

1,2,3,21

-1

2

2

 

抛出异常

16

1,2,3,22

-1

-2

2

 

抛出异常

17

1,2,3,23

-2

-2

-2

 

抛出异常

18

1,2,3,4,24

111

99

99

 

抛出异常

19

1,2,3,4,25

111

111

99

 

抛出异常

20

1,2,3,4,26

111

111

111

 

抛出异常

21

1,2,3,4,27

2

3

5

 

不构成三角形

22

1,2,3,4,28

5

2

3

 

不构成三角形

23

1,2,3,4,29

2

5

3

 

不构成三角形

 

 

 

 

 

 

一般边界值4n+1

仅考虑有效区间单个变量边界值(一般边界值):用最小值、略高于最小值、正常值、略低于最大值和最大值。

 

 

一般边界分析值测试用例

 
 

测试用例编号

数据列表

预期输出

 

a

b

c

 

1

1

50

50

等腰三角形

 

2

2

50

50

等腰三角形

 

3

50

50

50

等边三角形

 

4

99

50

50

等腰三角形

 

5

100

50

50

不构成三角形

 

6

50

1

50

等腰三角形

 

7

50

2

50

等腰三角形

 

8

50

99

50

等腰三角形

 

9

50

100

50

不构成三角形

 

10

50

50

1

等腰三角形

 

11

50

50

2

等腰三角形

 

12

50

50

99

等腰三角形

 

13

50

50

100

不构成三角形

 

 

 

 

 

 

 

健壮边界值6n+1

同时考虑有效区间和无效区间单个变量边界值(健壮边界值):除了最小值、略高于最小值、正常值、略低于最大值、最大值,还要有略超过最大值和略小于最小值的值。

 

健壮边界分析值测试用例

 
 

测试用例编号

数据列表

预期输出

 

a

b

c

 

1

0

50

50

抛出异常

 

2

1

50

50

等腰三角形

 

3

2

50

50

等腰三角形

 

4

50

50

50

等边三角形

 

5

99

50

50

等腰三角形

 

6

100

50

50

不构成三角形

 

7

101

50

50

抛出异常

 

8

50

0

50

抛出异常

 

9

50

1

50

等腰三角形

 

10

50

2

50

等腰三角形

 

11

50

99

50

等腰三角形

 

12

50

100

50

不构成三角形

 

13

50

101

50

抛出异常

 

14

50

50

0

抛出异常

 

15

50

50

1

等腰三角形

 

16

50

50

2

等腰三角形

 

17

50

50

99

等腰三角形

 

18

50

50

100

不构成三角形

 

19

50

50

101

抛出异常

 

被测试代码

被测试类TriangleProblem.class

@Component
public class TriangleProblem {


    public void determine(int a,int b,int c){

        if(a<=0||b<=0||c<=0) {
            throw new IllegalArgumentException("请输入大于0的边长");
        }
        else if (a>100||b>100||c>100) {
            throw new IllegalArgumentException("请输入小于101的边长");
        }
        else
            if (a + b >c && a + c >b && b + c >a) {// 判断是否三角形
            // 判断三角形类型
            if (a == b && b == c && c == a) {
                System.out.println("等边三角形");
            } else if (a == b || a == c || b == c) {
                System.out.println("等腰三角形");
            }else{System.out.println("普通三角形");}
        } else {
            System.out.println("非三角形");
        }
    }
}

 

测试代码

测试类
public class TriangleProblemTest {
    static class TestCase{
        int a, b, c;
        String t;
        Class<? extends Exception> e;

        public TestCase(int a,int b,int c,String t) {
            this.a = a;
            this.b = b;
            this.c = c;
            this.t = t;
        }

        TestCase(int a,int b,int c,Class<? extends Exception> e){
            this.a = a;
            this.b = b;
            this.c = c;
            this.e = e;
        }
    }

    @ParameterizedTest
    @MethodSource("valid_equivalence_class")
    public void valid_equivalence_class(TestCase tc){
        TriangleProblem problem = new TriangleProblem();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(stream));
        problem.determine(tc.a,tc.b,tc.c);
        assertEquals(tc.t , stream.toString().trim());
    }

    static Stream<TestCase> valid_equivalence_class(){
        return Stream.of(
                new TestCase(2,3,4,"普通三角形"),
                new TestCase(2,2,3,"等腰三角形"),
                new TestCase(2,3,2,"等腰三角形"),
                new TestCase(3,2,2,"等腰三角形"),
                new TestCase(2,2,2,"等边三角形"),
                new TestCase(1,2,3,"非三角形"),
                new TestCase(2,3,5,"非三角形"),
                new TestCase(5,2,3,"非三角形"),
                new TestCase(2,5,3,"非三角形")
        );
    }

  @ParameterizedTest
  @MethodSource("invalid_equivalence_class")
    public void test_valid_equivalence_class(TestCase tc){
      TriangleProblem problem = new TriangleProblem();
      assertThrows(tc.e,()->problem.determine(tc.a,tc.b,tc.c));
  }

  static Stream<TestCase> invalid_equivalence_class(){
        return Stream.of(
                new TestCase(0,2,3,IllegalArgumentException.class),
                new TestCase(0,0,2,IllegalArgumentException.class),
                new TestCase(0,0,0,IllegalArgumentException.class),
                new TestCase(-1,2,2,IllegalArgumentException.class),
                new TestCase(-1,-2,2,IllegalArgumentException.class),
                new TestCase(-2,-2,2,IllegalArgumentException.class),
                new TestCase(111,99,99,IllegalArgumentException.class),
                new TestCase(111,111,99,IllegalArgumentException.class),
                new TestCase(111,111,111,IllegalArgumentException.class)
        );
  }
}

 

posted @ 2018-04-26 23:31  qianyoo  阅读(7391)  评论(0编辑  收藏  举报