[TIP] REG: Changing standard test discovery

arun kali raja arunsep886 at gmail.com
Thu May 17 11:58:14 PDT 2018


Hi Bruno,

The testpaths option is to search for tests when no path is specified. So
that will not be useful. Also the modifyitems hooks is too late for me..
since item objects have to be first generated for the ICs that have to
added.

But i found a rather cool feature which i dint know existed at all.. i dint
read any documentation mentioning this..

"I just imported the testcases from the paths Y and Z into my X" :)

This opens up a lot more possibilities for developing my use case.

for example:

*testA/*
*    conftest.py*
*    test_A.py*
*testB/*
*    conftest.py*
*    test_B.py*

I have a dummy testcase in test_A and test_B

*import pytest*

*def test_valuesB(param1):*
*    assert True,"value is {}".format(param1)*


The conftest has a generate_tests hook that parameterizes the *"param1"*

*testA/conftest.py*

*import pytest*

*def pytest_generate_tests(metafunc):*
*    if 'param1' in metafunc.fixturenames:*
*        metafunc.parametrize("param1", range(10),indirect=False)*


*testB/conftest.py*

*import pytest*

*def pytest_generate_tests(metafunc):*
*    if 'param1' in metafunc.fixturenames:*
*        metafunc.parametrize("param1", range5),indirect=False)*

When i trigger pytest:

*$ pytest -vs tests**
*================================================================ test
session starts
================================================================*

*collected 15 items
                                                                          *

*testA/test_A.py::test_valuesA[0] PASSED*
*testA/test_A.py::test_valuesA[1] PASSED*
*testA/test_A.py::test_valuesA[2] PASSED*
*testA/test_A.py::test_valuesA[3] PASSED*
*testA/test_A.py::test_valuesA[4] PASSED*
*testA/test_A.py::test_valuesA[5] PASSED*
*testA/test_A.py::test_valuesA[6] PASSED*
*testA/test_A.py::test_valuesA[7] PASSED*
*testA/test_A.py::test_valuesA[8] PASSED*
*testA/test_A.py::test_valuesA[9] PASSED*
*testB/test_B.py::test_valuesB[0] PASSED*
*testB/test_B.py::test_valuesB[1] PASSED*
*testB/test_B.py::test_valuesB[2] PASSED*
*testB/test_B.py::test_valuesB[3] PASSED*
*testB/test_B.py::test_valuesB[4] PASSED*

Now when i imported test_valuesA into test_B.py like below:

*import pytest*
*from testA.test_A import test_valuesA*
*def test_valuesB(param1):*
*    assert True, "value is {}".format(param1)*

and then execute only test_B.py:

*$ pytest -vs testsB/test_B.py *
*================================================================ test
session starts
================================================================*

*collected 10 items
                                                                          *

*testB/test_B.py::test_valuesA[0] <- tests/test_tcA.py PASSED*
*testB/test_B.py::test_valuesA[1] <- tests/test_tcA.py PASSED*
*testB/test_B.py::test_valuesA[2] <- tests/test_tcA.py PASSED*
*testB/test_B.py::test_valuesA[3] <- tests/test_tcA.py PASSED*
*testB/test_B.py::test_valuesA[4] <- tests/test_tcA.py PASSED*
*testB/test_B.py::test_valuesB[0] PASSED*
*testB/test_B.py::test_valuesB[1] PASSED*
*testB/test_B.py::test_valuesB[2] PASSED*
*testB/test_B.py::test_valuesB[3] PASSED*
*testB/test_B.py::test_valuesB[4] PASSED*

Pytest has generated tests according to the conftest present in the *testB
folder and *pytest even seems to recognize that its imported from some
other location and displays that @the end result.

This lets me to over-ride the fixture implementation in the original file
and assign a new behavior/return value.

This is a really cool way to reuse testcases and totally solves my usecase..

Regards
Arun Kaliraja.B

On 16 May 2018 at 20:54, Bruno Oliveira <nicoddemus at gmail.com> wrote:

> Hi Arun,
>
> There's the `testpaths` variable that might be useful, otherwise I'm sure
> you can cook something yourself using some hooks, specially
> `pytest_collection_modifyitems`.
>
> Hope this helps,
> Bruno
>
> On Wed, May 16, 2018 at 11:52 AM arun kali raja <arunsep886 at gmail.com>
> wrote:
>
>> Hi,
>>
>> The pytest page on changing test discovery
>> <https://docs.pytest.org/en/latest/example/pythoncollection.html>  has
>> information on how to ignoring a set of paths/testcases.
>>
>> I want to do something reverse.. I have organised the tests in multiple
>> folders in terms of the codeArea  they cover.
>>
>> If the user triggers tests of codeArea say 'X',  i have some identified
>> codeAreas 'Y' and 'Z' whose tests should also be executed.
>>
>> How to achieve this in pytest?
>>
>> --
>> Regards
>> Arun kaliraja.B
>>
>


-- 
அன்புடன்,
பா. அருண் காளி ராசா
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20180518/430d0f53/attachment-0001.html>


More information about the testing-in-python mailing list