pytest

Is it possible to parametrize a test with fixtures?

房东的猫 提交于 2019-12-20 03:25:28
问题 I would like to pass @pytest.mark.parametrize not particular values but fixtures. Like so. Given a conftest with: @pytest.fixture def name1(): return 'foo' @pytest.fixture def name2(): return 'bar' within my test.py this works of course: @pytest.mark.parametrize('name', ['foo', 'bar', 'baz']) def test_name(name): print(name) This does not: @pytest.mark.parametrize('name', [name1, name2]) def test_name(name): print(name) I am aware that in this trivial case I could just create one name fixture

pytest文档11-assert断言

喜欢而已 提交于 2019-12-19 21:27:31
前言 断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢? 简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试 failed assert pytest允许您使用标准Python断言来验证Python测试中的期望和值。例如,你可以写下 # content of test_assert1.py def f(): return 3 def test_function(): assert f() == 4 断言f()函数的返回值,接下来会看到断言失败,因为返回的值是3,判断等于4,所以失败了 $ pytest test_assert1.py =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item test_assert1.py F [100%] ================================= FAILURES ========================

How to test Python classes that depend on argparse?

对着背影说爱祢 提交于 2019-12-19 08:59:15
问题 The below paste contains relevant snippets from three separate Python files. The first is a script called from the command line which instantiates CIPuller given certain arguments. What happens is that the script gets called with something like: script.py ci (other args to be swallowed by argparse). The second is part of a subclass called Puller . The third is part of a subclass of Puller called CIPuller . This works wonderfully, as the correct subclass is called, and any user using the wrong

TypeError: attrib() got an unexpected keyword argument 'convert'

旧街凉风 提交于 2019-12-18 18:33:23
问题 This error occurred during automated testing of a python project on the CI server using pytest . I'm using pytest==4.0.2 . This error only just started to occur, previous pipelines seem to work fine. The full error: File "/usr/local/lib/python3.7/site-packages/_pytest/tmpdir.py", line 35, in TempPathFactory lambda p: Path(os.path.abspath(six.text_type(p))) TypeError: attrib() got an unexpected keyword argument 'convert' 回答1: pytest seems to have the package attrs as a dependency. attrs==19.2

Can params passed to pytest fixture be passed in as a variable?

半城伤御伤魂 提交于 2019-12-18 15:48:34
问题 I have two simple test setups and I'm trying to group them in one fixture and want the test function to pass in the 'params' to the fixture. Here's a contrived example, to explain my question. Say I have the following pytest fixture: @pytest.fixture(scope="module", params=['param1','param2']) def myFixture(request): if request.param == 'param1': p = 5 elif request.param == 'param2': p = 10 return p # would like to set request.param = ['param1'] for myFixture def test_madeup(myFixture): assert

Can't get pytest to understand command-line arguments on setups

蓝咒 提交于 2019-12-18 13:26:31
问题 So I have been trying to get pytest to run selenium tests on different environments based on some command-line argument. But it keeps throwing this error: TypeError: setup_class() takes exactly 2 arguments (1 given) It seems that it is understanding that setup_class takes 2 arguments, but host is not being passed. Here's the code for setup_class : def setup_class(cls, host): cls.host = host And here is the conftest.py file: def pytest_addoption(parser): parser.addoption("--host", action=

Debugging pytest post mortem exceptions in pycharm/pydev

寵の児 提交于 2019-12-18 13:16:29
问题 I would like to use the built in Pytest runner of PyCharm together with the debugger without pre-configuring breakpoints. The problem is that exceptions in my test are caught by Pytest so PyCharm's post mortem debugger cannot handle the exception. I know using a breakpoint works but I would prefer not to run my test twice. Found a way to do this in Unittest, I would like to know if something like this exists in Pytest. Is there a way to catch unittest exceptions with PyCharm? 回答1: Are you

Writing a pytest function for checking the output on console (stdout)

你离开我真会死。 提交于 2019-12-18 12:52:38
问题 This link gives a description how to use pytest for capturing console outputs. I tried on this following simple code, but I get error import sys import pytest def f(name): print "hello "+ name def test_add(capsys): f("Tom") out,err=capsys.readouterr() assert out=="hello Tom" test_add(sys.stdout) Output: python test_pytest.py hello Tom Traceback (most recent call last): File "test_pytest.py", line 12, in <module> test_add(sys.stdout) File "test_pytest.py", line 8, in test_add out,err=capsys

How to use pytest to check that Error is NOT raised

风格不统一 提交于 2019-12-18 12:44:45
问题 Let's assume we have smth like that : import py, pytest ERROR1 = ' --- Error : value < 5! ---' ERROR2 = ' --- Error : value > 10! ---' class MyError(Exception): def __init__(self, m): self.m = m def __str__(self): return self.m def foo(i): if i < 5: raise MyError(ERROR1) elif i > 10: raise MyError(ERROR2) return i # ---------------------- TESTS ------------------------- def test_foo1(): with pytest.raises(MyError) as e: foo(3) assert ERROR1 in str(e) def test_foo2(): with pytest.raises

How to use pytest to check that Error is NOT raised

与世无争的帅哥 提交于 2019-12-18 12:44:22
问题 Let's assume we have smth like that : import py, pytest ERROR1 = ' --- Error : value < 5! ---' ERROR2 = ' --- Error : value > 10! ---' class MyError(Exception): def __init__(self, m): self.m = m def __str__(self): return self.m def foo(i): if i < 5: raise MyError(ERROR1) elif i > 10: raise MyError(ERROR2) return i # ---------------------- TESTS ------------------------- def test_foo1(): with pytest.raises(MyError) as e: foo(3) assert ERROR1 in str(e) def test_foo2(): with pytest.raises