我正在使用py.test并想知道是否/如何setup在运行每个测试之前检索在调用的方法中当前执行的测试的名称.考虑以下代码:
class TestSomething(object):
def setup(self):
test_name = ...
def teardown(self):
pass
def test_the_power(self):
assert "foo" != "bar"
def test_something_else(self):
assert True
Run Code Online (Sandbox Code Playgroud)
在TestSomething.test_the_power执行之前,我希望能够访问setup代码中概述的此名称,test_name = ...以便test_name== "TestSomething.test_the_power".
实际上,setup我为每个测试分配了一些资源.最后,看一下各种单元测试创建的资源,我希望能够看到哪一个是通过哪个测试创建的.最好的方法是在创建资源时使用测试名称.
我有一个奇怪的问题tox,py.test,coverage和pytest-cov:当py.test与--cov选项是从启动tox,这似乎需要__init__.py的文件tests是不会立即明显的文件夹.
在撰写这篇文章时,我通过添加上述内容解决了最初的问题tests/__init__.py,但到目前为止我还没有完全理解为什么它确实有效或无效,所以我仍然在寻求帮助.请参阅下面的详细信息.
我在SO上找到了一个相关的问题,但它只会让它更加混乱,因为答案似乎与我到目前为止所得到的相反: `py.test`和`__init __.py`文件
另请参阅此处的官方文档:py.test - 良好的集成实践(页面底部).
简化的项目结构:
setup.py
tox.ini
.coveragerc
project/
__init__.py
module1.py
module2.py
tests/
__init__.py (optional, an empty file)
test_module1.py
test_module2.py
Run Code Online (Sandbox Code Playgroud)
相关部分tox.ini:
[testenv:check]
commands = py.test --cov=project --cov-report=term
deps =
pytest
coverage
pytest-cov
[pytest]
python_files = test_*.py
norecursedirs = .tox
Run Code Online (Sandbox Code Playgroud)
相关部分.coveragerc:
[run]
branch = True
omit = project/tests/*
Run Code Online (Sandbox Code Playgroud)
现在,结果如下:
py.test --cov=project …当使用与pytest fixture集成的mock包中的patch decorator时,我遇到了一些神秘的东西.
我有两个模块
-----test folder
-------func.py
-------test_test.py
Run Code Online (Sandbox Code Playgroud)
在func.py中:
def a():
return 1
def b():
return a()
Run Code Online (Sandbox Code Playgroud)
在test_test.py中
import pytest
from func import a,b
from mock import patch,Mock
@pytest.fixture(scope="module")
def brands():
return 1
mock_b=Mock()
@patch('test_test.b',mock_b)
def test_compute_scores(brands):
a()
Run Code Online (Sandbox Code Playgroud)
似乎补丁装饰与pytest fixture不兼容.有没有人对此有所了解?Tnanks
我已经确定了一些长期运行的pytest测试
py.test --durations=10
Run Code Online (Sandbox Code Playgroud)
我想用line_profiler或cprofile之类的东西来测试其中一个测试.我真的想从测试本身获取配置文件数据,因为pytest设置或拆除很可能是慢速的一部分.
但是考虑到line_profiler或cprofile通常是如何涉及的,我不清楚如何使它们与pytest一起工作.
测试功能我需要传递参数并查看输出与预期输出匹配.
当函数的响应只是一个小数组或可以在测试函数中定义的单行字符串时很容易,但假设我测试的函数修改了一个可能很大的配置文件.或者,如果我明确定义它,结果数组就会长4行.我在哪里存储,以便我的测试保持干净,易于维护?
现在如果那是字符串我只是在.py测试附近放一个文件并open()在测试中进行:
def test_if_it_works():
with open('expected_asnwer_from_some_function.txt') as res_file:
expected_data = res_file.read()
input_data = ... # Maybe loaded from a file as well
assert expected_data == if_it_works(input_data)
Run Code Online (Sandbox Code Playgroud)
我发现这种方法存在许多问题,比如维护此文件的最新问题.它看起来也很糟糕.我可以把事情变得更好:
@pytest.fixture
def expected_data()
with open('expected_asnwer_from_some_function.txt') as res_file:
expected_data = res_file.read()
return expected_data
@pytest.fixture
def input_data()
return '1,2,3,4'
def test_if_it_works(input_data, expected_data):
assert expected_data == if_it_works(input_data)
Run Code Online (Sandbox Code Playgroud)
这只是将问题移到另一个地方,通常我需要测试函数是否在空输入,输入单个项目或多个项目的情况下工作,所以我应该创建一个大的夹具,包括所有三个案例或多个灯具.最后代码变得相当混乱.
如果一个函数需要一个复杂的字典作为输入或者给出相同大小的字典测试代码变得丑陋:
@pytest.fixture
def input_data():
# It's just an example
return {['one_value': 3, 'one_value': 3, 'one_value': 3,
'anotherky': 3, 'somedata': 'somestring'],
['login': 3, 'ip_address': …Run Code Online (Sandbox Code Playgroud) 在pytest的文档中列出了测试用例的各种示例.他们中的大多数都展示了功能测试.但我错过了一个如何测试类和类方法的例子.假设我们在cool.py要测试的模块中有以下类:
class SuperCool(object):
def action(self, x):
return x * x
Run Code Online (Sandbox Code Playgroud)
相应的测试课如何tests/test_cool.py看?
class TestSuperCool():
def test_action(self, x):
pass
Run Code Online (Sandbox Code Playgroud)
怎么test_action()用来测试action()?
我想pytest并行运行所有测试而不是顺序运行.
我目前的设置如下:
class Test1(OtherClass):
@pytest.mark.parametrize("activity_name", ["activity1", "activity2"])
@pytest.mark.flaky(reruns=1)
def test_1(self, activity_name, generate_test_id):
"""
"""
test_id = generate_random_test_id()
test_name = sys._getframe().f_code.co_name
result_triggers = self.proxy(test_name, generate_test_id, test_id, activity_name)
expected_items = ["response"]
validate_response("triggers", result_triggers, expected_items)
@pytest.mark.parametrize("activity_name", ["activity1", "activity2"])
@pytest.mark.flaky(reruns=1)
def test_2(self, activity_name, generate_test_id):
"""
"""
#same idea...
Run Code Online (Sandbox Code Playgroud)
我用我的测试pytest -v -s.
结果是我的测试按顺序运行,这需要花费大量时间,因为其中一些测试等待来自远程服务器的响应(集成测试).
有没有办法并行运行pytest?
我是 Python 开发新手,我正在使用pytest编写测试用例,我需要模拟一些行为。谷歌搜索pytest 的最佳模拟库,只会让我感到困惑。我见过unittest.mock、mock、mocker 和pytest-mock。不太确定该使用哪一个。有人可以解释一下它们之间的区别并向我推荐一个吗?
我需要测试使用的功能datetime.datetime.now().最简单的方法是什么?
我有py.test运行的多个测试,它们位于多个文件的多个类中.
共享一个大字典的最简单的方法是什么 - 我不想复制 - py.test使用的每个文件中每个类的每个方法?
简而言之,我需要为每个测试制作一个"全局变量".在py.test之外,我没有使用这个变量,所以我不想将它存储在被测试的文件中.我经常使用py.test的灯具,但这似乎有点过分了.也许这是唯一的方法?
pytest ×10
python ×10
mocking ×2
coverage.py ×1
cprofile ×1
datetime ×1
pytest-mock ×1
tdd ×1
testing ×1
tox ×1
unit-testing ×1