编写一个方法
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if self.is_empty():
return None
return self.items.pop()
def peek(self):
if self.is_empty():
return None
return self.items[-1]
def is_empty(self):
return len(self.items) == 0
创建一个py文件,编写以下测试类调用上面的方法
import pytest
from python自动化测试.review.stack import Stack
def test_push():
s = Stack()
assert s.push(1) == s.pop()
def test_pop():
s = Stack()
s.push(1)
s.push(2)
s.push(3)
assert s.pop() == 1
assert s.pop() == 2
assert s.pop() == 3
def test_peek():
s = Stack()
s.push(1)
s.push(2)
s.push(3)
assert s.peek() == 3
def test_is_empty():
s = Stack()
assert s.is_empty() == True
assert s.is_empty() == False
if __name__ == '__main__':
pytest.main()
终端Terminal执行如下代码可生成对应测试报告
#(1)正确生成XML测试报告
pytest StackTest.py --junitxml=./report.xml
#(2)正确在线测试报告
pytest StackTest.py --pastebin=all
#(3)生成html报告
pip install pytest-html
pytest StackTest.py --html=./report.html # ./表示当前路径