[TIP] pytest
Bruno Oliveira
nicoddemus at gmail.com
Fri Oct 14 11:50:35 PDT 2016
Hi James,
Thanks for posting the complete example.
There are some problems here, both general Python problems and some related
to how you are using pytest:
1. testscript.py has no access to the A function because it doesn’t
import it.
2. You probably want to declare your fixture in a conftest.py file so
pytest can make it available for all tests in your test suite.
3. You have to first declare your option using the pytest_addoption hook.
4. Since you intend to provide the value passed in the command line to
the tests using a fixture, it is simpler to skip the test in the fixture
itself.
Here’s the full example with my suggestions:
# conftest.pyimport pytest
@pytest.fixture(scope='module')def a(request):
value = request.config.getoption("A")
if not value:
pytest.skip('test needs -A option to run')
return value
def pytest_addoption(parser):
parser.addoption("--A", action="store", default=None, help="a option")
# testscript.pydef test_foo(a):
assert a == '1'
Running it:
$ py.test testscript.py -q
s
1 skipped in 0.01 seconds
$ py.test testscript.py -q --A 1
.
1 passed in 0.01 seconds
$ py.test testscript.py -q --A 2
F
================================== FAILURES ===================================
__________________________________ test_foo ___________________________________
a = '2'
def test_foo(a):
> assert a == '1'
E assert '2' == '1'
E - 2
E + 1
testscript.py:6: AssertionError
1 failed in 0.07 seconds
Hope that helps,
Bruno.
On Fri, Oct 14, 2016 at 3:07 PM James <bjlockie at lockie.ca> wrote:
Here is the exact code:
$ cat testconfig.py
import pytest
@pytest.fixture(scope='module')
def A(request):
return request.config.getoption("--A")
$ cat testscript.py
import pytest
skip_A = pytest.mark.skipif(
A(pytest.config.request),
reason="need --A option to run"
)
$ py.test testscript.py
============================= test session starts
==============================
platform linux2 -- Python 2.7.12, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /home/rjl/pytest, inifile:
collected 0 items / 1 errors
==================================== ERRORS
====================================
________________________ ERROR collecting testscript.py
________________________
testscript.py:4: in <module>
A(pytest.config.request),
E NameError: name 'A' is not defined
=========================== 1 error in 0.01 seconds
============================
Instead of doing
pytest.config.getoption more than one like this:
skip_A = pytest.mark.skipif( pytest.config.getoption("--A"), reason="need
--A option to run" )
I want to do it once in the config file.
I might want to use the value of the argument --A elsewhere so I don't want
to do getoption many times.
On 2016-10-14 01:16 PM, Bruno Oliveira wrote:
Hi,
After fixing the missing “pytest” import, I get this error:
foo.py:8: in <module>
A(pytest.config.request),
E AttributeError: 'Config' object has no attribute 'request'
Please make sure to post a complete example and state clearly what your
problem is and what you are trying to accomplish.
Cheers,
Bruno.
On Fri, Oct 14, 2016 at 2:03 PM James <bjlockie at lockie.ca> wrote:
I have this in my config file:
@pytest.fixture(scope='module')
def A(request):
return request.config.getoption("--A")
skip_A = pytest.mark.skipif(
A(pytest.config.request),
reason="need --A option to run"
)
but when I run it, it says
E NameError: name 'A' is not defined
_______________________________________________
testing-in-python mailing list
testing-in-python at lists.idyll.org
http://lists.idyll.org/listinfo/testing-in-python
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161014/6f0ebf6b/attachment-0001.htm>
More information about the testing-in-python
mailing list