(四)运行单个测试用例,命令行选项

运行单个测试用例可以直接在指定文件后面添加   ::test_name


命令行选项

  使用pytest --help可以看到全部的选项

--collect-only

  使用该选项可以展示在给定的配置下哪些测试用例会被运行,可以方便的在运行测试之前检查选中的测试用例是否符合预期


-k

  该选项允许你使用表达式指定希望运行的测试用例。如果某测试名是唯一的,或者多个测试名的前缀或者后缀相同,那么可以使用表达式来快速定位

  筛选 test_passing()和test_asdict() ,使用--collect-only来验证筛选情况

  去掉--collect-only运行

   查看详细信息,看看是否运行的是我们筛选的测试


-m

  marker(标记),用于标记测试并分组,以便快速选中运行

  

  test_replace()和test_passing(),这两个都不在同一个文件夹下,要通知选中他们,预先做好标记

  标记名随意,如run_these_pleae,那么就用@pytest.mark.run_these_please这样名字的装饰器做标记,有相同标记的测试是一个集合,可以一起运行

  首先导入pytest,然后给函数加装饰器

test_one.py

import pytest

@pytest.mark.run_these_please
def test_passing():
	assert (1, 2, 3) == (1, 2, 3)

  

test_four.py

import collections
import pytest
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

@pytest.mark.run_these_please
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

  

运行

猜你喜欢

转载自www.cnblogs.com/Mr-chenshuai/p/10395563.html