python - 如何自定义pytest名称
问题描述
我想自定义我的 pytest 的输出名称以包含我的装置的名称
所以我有
def test_t1(
when_creating_a_project_from_a_sales_handoff,
with_a_new_customer,
and_no_conflicting_data_exists,
create_project):
it_will_create_a_customer_with_the_releavant_information()
it_will_create_a_project_that_references_the_newly_created_customer()
我希望显示的测试名称是某个版本
when_creating_a_project_from_a_sales_handoff
with_a_new_customer
and_no_conflicting_data_exists
create_project
我怎样才能做到这一点?我尝试创建
@fixture
def namer(request):
request.node.name = 'test_foo'
但没有骰子,它没有改变测试显示名称
解决方案
我设法更改了显示的名称,但只能使用 pytest 私有变量。
制作一个conftest.py
文件并制作此功能:
def pytest_itemcollected(item):
""" change test name, using fixture names """
item._nodeid = ', '.join(item._fixtureinfo.argnames)
当我用 pytest 运行这个 test_file 时:
import pytest
@pytest.fixture()
def fixture_1():
pass
@pytest.fixture()
def fixture_2():
pass
def test1(fixture_1):
assert 1 == 1
def test_a(fixture_1, fixture_2):
assert 1 == 2
结果是:
pytest
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
rootdir: C:\Users\gelineau\Desktop\kaggle_flavien, inifile:
collected 2 items
fixture_1 . [ 50%]
fixture_1, fixture_2 F [100%]
================================== FAILURES ===================================
___________________________________ test_a ____________________________________
fixture_1 = None, fixture_2 = None
def test_a(fixture_1, fixture_2):
> assert 1 == 2
E assert 1 == 2
test\test_so.py:15: AssertionError
===================== 1 failed, 1 passed in 0.86 seconds ======================
新的测试名称也打印在 pycharm 中
推荐阅读
- c++ - 在 C++11 标准中,为什么要依赖 char 类型的实现?
- user-interface - 是否可以在 Google Apps 脚本中获取其他人的 UI?
- javascript - 提高我的脚本性能和速度 - Google 应用脚本
- java - Java 类无法与 Mysql 驱动程序连接
- mysql - 终端上的 MySQL 对 web 上的 PHPmyAdmin 没有影响
- html - 插入特定代码使其看起来像一条细小的细线
- javascript - 如何在javascript中附加table>tr in table>tbody(已经存在的表)(使用Chrome扩展)
- node.js - 将 Node 应用程序从开发打包到生产
- sql - 从雪花中卸载 CSV,其中只有非空值用引号括起来
- php - 拒绝除 php 文件 .htaccess 之外的所有流量