[TIP] pytest
James
bjlockie at lockie.ca
Mon Oct 17 10:25:46 PDT 2016
Great, thanks.
On 2016-10-17 11:00 AM, Bruno Oliveira wrote:
>
> On Mon, Oct 17, 2016 at 11:42 AM James bjlockie at lockie.ca
> <http://mailto:bjlockie@lockie.ca> wrote:
>
> Is it possible to skip all the tests within a class without having
> to pass "a" into all the functions?
>
> You can use the |usefixtures| marker on the class:
>
> |@pytest.mark.usefixtures('a') class Test: def test_foo(self): pass |
>
> Cheers,
> Bruno.
>
> On 2016-10-16 12:56 AM, Bruno Oliveira wrote:
>>
>> Hi James,
>>
>> What error do you get? From your example it is missing a |self|
>> argument, otherwise it should work just fine.
>>
>> Cheers,
>> Bruno.
>>
>>
>>
>> On Sat, Oct 15, 2016 at 11:03 AM James <bjlockie at lockie.ca
>> <mailto:bjlockie at lockie.ca>> wrote:
>>
>> I tried to make it work with a class:
>>
>> import pytest
>>
>> # testscript.py
>> class TestAclasss:
>>
>>
>> def test_foo(a):
>> assert a == '1'
>>
>> Do I need to do something in the __init__ function?
>>
>>
>>
>>
>> On 2016-10-14 02:50 PM, Bruno Oliveira wrote:
>>>
>>> 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.py import 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.py def 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
>>> <mailto: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.getoptionmore 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 <mailto: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
>>>> <mailto: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/20161017/f3714e48/attachment-0001.htm>
More information about the testing-in-python
mailing list