使用 Pytest 执行 BVA 测试
先决条件 - BVA 测试
要执行自动 BVA(边界值分析)测试,我们可以使用Python中的 Pytest 或 Unittest 库。在这里,我们将使用 Pytest 库来执行为一个简单程序编写的测试用例。
当给定边 A、B 和 C 的长度时,我们将为确定三角形类型(即等边、等腰、不等边或如果不可能形成三角形)的程序执行 BVA 测试。此外,长度的范围在 10 到 50 之间(包括两者)。
triangletype.py文件中的代码:
Python3
# import math
class Error(BaseException):
pass
class OutOfRangeError(Error):
def __init__(self, message):
self.message = message
class TriangleError(Error):
def __init__(self, message):
self.message = message
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')
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')
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
from triangletype import triangleType
from triangletype import OutOfRangeError
def test_bva_minm_a():
with pytest.raises(OutOfRangeError):
triangleType(9, 20, 15)
def test_bva_minm_b():
with pytest.raises(OutOfRangeError):
triangleType(20, 9, 15)
def test_bva_minm_c():
with pytest.raises(OutOfRangeError):
triangleType(20, 15, 9)
def test_bva_min_a():
assert triangleType(10, 20, 15) == "Scalene Triangle"
def test_bva_min_b():
assert triangleType(20, 10, 15) == "Scalene Triangle"
def test_bva_min_c():
assert triangleType(15, 20, 10) == "Scalene Triangle"
def test_bva_minp_a():
assert triangleType(11, 20, 15) == "Scalene Triangle"
def test_bva_minp_b():
assert triangleType(20, 11, 15) == "Scalene Triangle"
def test_bva_minp_c():
assert triangleType(15, 20, 11) == "Scalene Triangle"
def test_bva_maxm_a():
assert triangleType(49, 30, 45) == "Scalene Triangle"
def test_bva_maxm_b():
assert triangleType(30, 49, 45) == "Scalene Triangle"
def test_bva_maxm_c():
assert triangleType(45, 30, 49) == "Scalene Triangle"
def test_bva_max_a():
assert triangleType(50, 30, 45) == "Scalene Triangle"
def test_bva_max_b():
assert triangleType(30, 50, 45) == "Scalene Triangle"
def test_bva_max_c():
assert triangleType(45, 30, 50) == "Scalene Triangle"
def test_bva_maxp_a():
with pytest.raises(OutOfRangeError):
triangleType(51, 30, 45)
def test_bva_maxp_b():
with pytest.raises(OutOfRangeError):
triangleType(30, 51, 45)
def test_bva_maxp_c():
with pytest.raises(OutOfRangeError):
triangleType(30, 45, 51)
def test_bva_nominal():
assert triangleType(12, 12, 12) == "Equilateral Triangle"
现在,我们需要使用 Pytest 库为上述程序编写测试用例。每个测试用例都写在一个单独的函数中,我们使用pytest.raises函数来检查给定的输入是有效还是无效。在 BVA 中,使用了 3 种技术:边界值检查 (BVC)、稳健性测试和最坏情况测试。在这里,我们将执行鲁棒性测试,其中对于n个输入变量,可以编写6n + 1 个测试用例。
注意:函数名称和测试文件名应始终以单词“test”开头。
In the above program,
n = 3
Since number of test cases = 6n + 1
Therefore,
Number of test cases = 6 * 3 + 1
= 19 test cases
因此,我们为上述程序编写了 19 个测试用例,分别对应以下值: min、min – 1、min + 1、max、max – 1、max + 1和标称值
test_triangletype_bva.py文件中的代码:
Python3
import pytest
from triangletype import triangleType
from triangletype import OutOfRangeError
def test_bva_minm_a():
with pytest.raises(OutOfRangeError):
triangleType(9, 20, 15)
def test_bva_minm_b():
with pytest.raises(OutOfRangeError):
triangleType(20, 9, 15)
def test_bva_minm_c():
with pytest.raises(OutOfRangeError):
triangleType(20, 15, 9)
def test_bva_min_a():
assert triangleType(10, 20, 15) == "Scalene Triangle"
def test_bva_min_b():
assert triangleType(20, 10, 15) == "Scalene Triangle"
def test_bva_min_c():
assert triangleType(15, 20, 10) == "Scalene Triangle"
def test_bva_minp_a():
assert triangleType(11, 20, 15) == "Scalene Triangle"
def test_bva_minp_b():
assert triangleType(20, 11, 15) == "Scalene Triangle"
def test_bva_minp_c():
assert triangleType(15, 20, 11) == "Scalene Triangle"
def test_bva_maxm_a():
assert triangleType(49, 30, 45) == "Scalene Triangle"
def test_bva_maxm_b():
assert triangleType(30, 49, 45) == "Scalene Triangle"
def test_bva_maxm_c():
assert triangleType(45, 30, 49) == "Scalene Triangle"
def test_bva_max_a():
assert triangleType(50, 30, 45) == "Scalene Triangle"
def test_bva_max_b():
assert triangleType(30, 50, 45) == "Scalene Triangle"
def test_bva_max_c():
assert triangleType(45, 30, 50) == "Scalene Triangle"
def test_bva_maxp_a():
with pytest.raises(OutOfRangeError):
triangleType(51, 30, 45)
def test_bva_maxp_b():
with pytest.raises(OutOfRangeError):
triangleType(30, 51, 45)
def test_bva_maxp_c():
with pytest.raises(OutOfRangeError):
triangleType(30, 45, 51)
def test_bva_nominal():
assert triangleType(12, 12, 12) == "Equilateral Triangle"
要执行上述测试用例,请在单个文件夹中创建两个单独的文件triangletype.py和test_triangletype_bva.py 。要执行编写以下命令:
pytest
或者
pytest -v
pytest -v显示详细输出。
输出如下所示:
正如我们在输出中看到的,所有 19 个测试用例都通过了。但是,如果我们以输入无效的方式编辑测试用例,那么测试用例将失败。例如,如果我们更改test_triangletype_bva.py文件中test_bva_max_c()测试用例中c变量的值,则输出如下: