代码拉取完成,页面将自动刷新
$ pip install pytest
$ pip list
$ pip show pytest
$ pytest --version
$ pytest --help
写一个加法函数,然后编写该函数的测试方法。
def add(x, y):
return x+y
def test1():
assert 3 == add(1, 1)
def test2():
assert 1 != add(1, 1)
$ pytest -vv test_func.py
or
$ pytest
计算显示每个测试执行的时间。
$ pytest --durations=0 -vv test_func.py
import pytest
def func(x):
if x == 0:
raise ValueError("value error!")
else:
pass
# func(1)
# try:
# func(0)
# except ValueError as identifier:
# print("error")
def test_mytest1():
with pytest.raises(ValueError):
func(0)
def test_mytest2():
assert func(1) == None
为同一个函数传递不同参数进行测试。
def add(x, y):
return x+y
import pytest
@pytest.mark.parametrize(
"x,y,expected",
[
(1, 1, 2),
(2, 2, 4),
(10, 10, 20),
]
)
def test_add(x, y, expected):
assert add(x, y) == expected
将测试方法分为不同的测试组,测试时可以单独测试某个组的方法。
$ pytest --markers
$ nano pytest.ini
...
[pytest]
markers =
g1: group1.
g2: group2.
...
$ pytest --markers
import pytest
@pytest.mark.g1
def test_func1():
pass
@pytest.mark.g2
def test_func2():
pass
@pytest.mark.g1
def test_func3():
pass
@pytest.mark.g2
def test_func4():
pass
@pytest.mark.g1
def test_func5():
pass
$ pytest -vv
$ pytest -vv -m g1
$ pytest -vv -m g2
https://gitee.com/komavideo/LearnPytest
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。