标签:用例 指定 test pytho efault 结构 strong 直接 cte
pytest有点很多,易读、易写、易运行、失败信息详细等等。
运行pytest

在test文件夹下创建
test_three.py
import collections
Task = collections.namedtuple("Task", ["name", "age", "salary"])
Task.__new__.__defaults__ = (None, None, None)
def test_defaults():
	t1 = Task()
	t2 = Task(None, None, None)
	assert t1 ==t2
def test_member_access():
	t = Task("bone", "26")
	assert t.name == "bone"
	assert t.age == "26"
	assert t.salary == None
test_four.py
import collections
Task = collections.namedtuple("Task", ["name", "age", "salary"])
Task.__new__.__defaults__ = (None, None, None)
def test_asdict():
	t_task = Task("bone", "26", "3000")
	t_dict = t_task._asdict()
	expected = {
		"name": "bone",
		"age": "26",
		"salary": "3000"
	}
	assert t_dict == expected
def test_replace():
	t_before = Task("bone", "26", "3000")
	t_after = t_before._replace(age="25", salary="222")
	t_expected = Task("bone", "25", "222")
	assert t_after == t_expected
此时目录结构如下:

在ch1直接运行pytest,没有指定参数,那么会将4个测试文件都运行

指定测试文件或者目录

pytest搜索测试文件和测试用例的过程为测试搜索(test discovery)
遵守pytest的命名规则,pytest就能自动搜索所有待执行 测试用例
命令规则:
测试文件和测试函数最好以test_开头,如果之前的测试用例遵循其他的命名规则,也可以修改默认的测试搜索规则
标签:用例 指定 test pytho efault 结构 strong 直接 cte
原文地址:https://www.cnblogs.com/Mr-chenshuai/p/10394761.html