📜  使用Pytest执行等效类测试

📅  最后修改于: 2021-08-25 10:17:01             🧑  作者: Mango

先决条件–等效类测试

为了执行自动的等效类测试,我们可以使用Pytest或Unittest Python库。在本文中,我们将使用Pytest库为一个简单的程序执行测试用例。

问题 :
在给定三角形三边A,B和C的长度的情况下,对确定三角形类型的程序执行等价测试。边长的范围在10到50(包括两端)之间。

三角型文件中的代码:

Python3
# import math
  
# parent class for Error
class Error(BaseException):
    pass
  
# child class of Error named OutOfRangeError
class OutOfRangeError(Error):
    def __init__(self, message):
        self.message = message
  
# child class of Error named TriangleError        
class TriangleError(Error):
    def __init__(self, message):
        self.message = message
  
# checks if variables are in range
# if variables not in range then OutOfRangeError is raised
def checkRange(a, b, c):
    if a<10 or a>50:
        raise OutOfRangeError('A is out of the given range')
    if b<10  or b>50:
        raise OutOfRangeError('B is out of the given range') 
    if c<10 or c>50:
        raise OutOfRangeError('C is out of the given range')
  
# checks if the given values of a, b, c can form a triangle
# if not, then Triangle error is raised
def checkTriangle(a, b, c):
    if a + b<= c or b + c<= a or c + a<= b:
        raise TriangleError('Triangle cannot be formed with these sides')
  
# determines the type of triangle        
def triangleType(a, b, c):
    checkRange(a, b, c)
    checkTriangle(a, b, c)
    # s = (a + b+c)/2
    # ar = math.sqrt(s*(s-a)*(s-b)*(s-c))
    # inradius = ar / s
    if(a == b and b == c):   
        return "Equilateral Triangle"
    elif(a == b or a == c or b == c):
        return "Isosceles Triangle"
    else:
        return "Scalene Triangle"
  
  
def main():
    try:
        print("Enter the sides of the triangle in range [10-50]")
  
        a = int(input('Enter Side A:'))
        b = int(input('Enter Side B:'))
        c = int(input('Enter Side C:'))
    except ValueError as v:
        print(v + " Raised :Input is not an integer.")
        exit(0)
    try:
        checkRange(a, b, c)
    except OutOfRangeError as e:
        print("OutOfRangeError:" + e.message)
      
    try:
        checkTriangle(a, b, c)
    except TriangleError as e:
        print('TriangleError:' + e.message)
  
    typeOfTriangle = triangleType(a, b, c)
  
    print("The triangle is: " + typeOfTriangle)
  
if __name__ == "__main__":
    main()


Python3
import pytest
  
# importing classes and function which we use in this file
from triangletype import OutOfRangeError
from triangletype import TriangleError
from triangletype import triangleType
  
# check if a < 10
def test_invalid_less_a():
    with pytest.raises(OutOfRangeError):
        triangleType(9, 20, 15)
  
# check if b < 10        
def test_invalid_less_b():
    with pytest.raises(OutOfRangeError):
        triangleType(20, 9, 15)
          
# check if c < 10        
def test_invalid_less_c():
    with pytest.raises(OutOfRangeError):
        triangleType(20, 15, 9)
  
  
# check if a > 50
def test_invalid_more_a():
    with pytest.raises(OutOfRangeError):
        triangleType(51, 30, 45)
          
# check if b > 50        
def test_invalid_more_b():
    with pytest.raises(OutOfRangeError):
        triangleType(30, 51, 45)
          
# check if c > 50        
def test_invalid_more_c():
    with pytest.raises(OutOfRangeError):
        triangleType(30, 45, 51)
  
# check if a, b, c can form a triangle or not        
def test_valid_invalidtriangle():
    with pytest.raises(TriangleError):
        triangleType(20, 15, 40)
  
# valid class - determines type of triangle        
def test_valid():
    assert triangleType(20, 15, 10) == "Scalene Triangle"


现在,我们需要使用Pytest库为上述程序编写测试用例。每个测试用例都编写在一个单独的函数中,在该函数,我们使用pytest.raises函数检查给定输入是否有效。对于上述程序,我们创建7个无效类1个有效类。7个无效的类是:

  • a <10
  • > 50
  • b <10
  • b> 50
  • 小于10
  • c> 50
  • 给定a,b,c的值不能形成三角形

1个有效的类别是:

  • 10 <= a,b,c <= 50

注意:函数名称和测试文件名称应始终以单词“ test”开头。

test_triangletype_eq.py文件中的代码:

Python3

import pytest
  
# importing classes and function which we use in this file
from triangletype import OutOfRangeError
from triangletype import TriangleError
from triangletype import triangleType
  
# check if a < 10
def test_invalid_less_a():
    with pytest.raises(OutOfRangeError):
        triangleType(9, 20, 15)
  
# check if b < 10        
def test_invalid_less_b():
    with pytest.raises(OutOfRangeError):
        triangleType(20, 9, 15)
          
# check if c < 10        
def test_invalid_less_c():
    with pytest.raises(OutOfRangeError):
        triangleType(20, 15, 9)
  
  
# check if a > 50
def test_invalid_more_a():
    with pytest.raises(OutOfRangeError):
        triangleType(51, 30, 45)
          
# check if b > 50        
def test_invalid_more_b():
    with pytest.raises(OutOfRangeError):
        triangleType(30, 51, 45)
          
# check if c > 50        
def test_invalid_more_c():
    with pytest.raises(OutOfRangeError):
        triangleType(30, 45, 51)
  
# check if a, b, c can form a triangle or not        
def test_valid_invalidtriangle():
    with pytest.raises(TriangleError):
        triangleType(20, 15, 40)
  
# valid class - determines type of triangle        
def test_valid():
    assert triangleType(20, 15, 10) == "Scalene Triangle" 

要执行上述测试用例,在一个文件夹中创建两个单独的文件triangletype.pytest_triangletype_eq.py 。要执行,请编写以下命令:

pytest

或者

pytest -v

pytest -v显示详细输出。

输出如下所示:

从上面的输出中可以看到,所有8个测试用例都通过了。但是,例如,如果我们编辑一个测试用例,则在test_triangletype_eq.py文件的test_valid()测试用例中,将c变量的值更改为90,则该测试用例将失败: