pytest和allure写一段测试代码
时间: 2023-08-30 17:10:43 浏览: 107
以下是一段使用pytest和allure写的测试代码示例:
```python
import allure
import pytest
@allure.feature("Calculator")
class TestCalculator:
@allure.story("Addition")
def test_addition(self):
result = 2 + 2
assert result == 4
allure.attach(name="Addition Result", body=str(result), attachment_type=allure.attachment_type.TEXT)
@allure.story("Subtraction")
def test_subtraction(self):
result = 5 - 3
assert result == 2
allure.attach(name="Subtraction Result", body=str(result), attachment_type=allure.attachment_type.TEXT)
@allure.story("Multiplication")
def test_multiplication(self):
result = 3 * 4
assert result == 12
allure.attach(name="Multiplication Result", body=str(result), attachment_type=allure.attachment_type.TEXT)
@allure.story("Division")
def test_division(self):
result = 10 / 2
assert result == 5
allure.attach(name="Division Result", body=str(result), attachment_type=allure.attachment_type.TEXT)
if __name__ == '__main__':
pytest.main(args=["-s", "-v", "--alluredir=allure-results"])
```
在这个示例中,我们定义了一个名为TestCalculator的测试类,其中包含四个测试方法:test_addition,test_subtraction,test_multiplication和test_division。每个测试方法都使用assert语句进行断言,并使用allure.attach函数将测试结果作为附件添加到测试报告中。我们还使用allure.feature和allure.story装饰器来组织测试用例。最后,我们使用pytest.main函数运行测试并将测试结果输出到allure-results目录中,以供后续生成测试报告使用。
阅读全文