定义加法被测函数,数据类型为:整型或者浮点型 数据区间为 [-99,99] 浮点数允许小数点后两位,在调用每个测试方法之前打印【开始计算】 在调用每个测试方法之后打印【结束计算】 调用完所有的测试用例最终输出【结束测试】 为用例添加hebeu标签 生成Allure测试报告
时间: 2023-12-03 13:44:19 浏览: 33
以下是一个可能的实现示例:
```python
import allure
@allure.feature('加法测试')
@allure.tag('hebeu')
def test_add():
with allure.step('整型加法测试'):
print('开始计算')
assert add(1, 2) == 3
print('结束计算')
with allure.step('浮点型加法测试'):
print('开始计算')
assert add(3.14, 2.86) == 6
print('结束计算')
print('结束测试')
def add(a, b):
if not (-99 <= a <= 99 and -99 <= b <= 99):
raise ValueError('超出数据区间')
return round(a + b, 2)
```
这个示例中,我们使用了 `allure` 库来生成测试报告。首先,我们使用 `@allure.feature` 和 `@allure.tag` 装饰器来标记我们的测试用例。然后,在每个测试用例中,我们使用 `with allure.step` 来记录测试步骤,并在开始和结束时打印相应的信息。最后,在所有测试用例执行完毕后,我们在函数外部打印一条结束测试的信息。
在 `add` 函数中,我们首先检查输入数据是否在 [-99,99] 的范围内,如果不是则抛出异常。然后,我们对两个数进行加法运算,并使用 `round` 函数保留两位小数。注意,在 `assert` 语句中我们使用了 `==` 运算符来检查两个数是否相等,这是因为浮点数的精度问题可能导致直接使用 `assert a + b == 6` 这样的语句会失败。
最后,我们可以使用 `pytest` 命令来运行测试,并使用 `--alluredir` 参数指定测试报告的输出路径:
```
pytest test_add.py --alluredir=allure-report
```
运行完毕后,我们可以使用 `allure serve` 命令来查看测试报告:
```
allure serve allure-report
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)