Hi Lele,
On Mon, Mar 13, 2017 at 10:44 AM Lele Gaifax <[email protected]> wrote:
> Hi all,
>
> I have a parametrized test function that loads a set of cases from the
> filesystem, something like
>
> @pytest.mark.parametrize('py_code',
> load_tests_from_directory('some_directory'))
> def test_foo(py_code):
> assert some_property(py_code)
>
> The `load_tests_from_directory()` loads Python snippets from the given
> directory and basically returns their code objects (that is, the result of
> `compile(snippet, 'exec')`).
>
> Now I have a new set of snippets that are Python 3.6 only (they use
> f-strings), and I would like to execute those tests only under Python 3.6+.
>
> I tried the obvious, that is
>
> @pytest.mark.skipif(sys.version_info < (3,6))
> @pytest.mark.parametrize('py_code',
> load_tests_from_directory('py36_specific_dir'))
> def test_foo_py36_only(py_code):
> assert some_property(py_code)
>
> but unfortunately that does not work, because the parametrization happens
> also
> when the skipif condition is met, and thus the compile() call crash.
>
> Is there a better alternative to recode the above like
>
> @pytest.mark.skipif(sys.version_info < (3,6))
> def test_foo_py36_only(py_code):
> for py_code in load_tests_from_directory('py36_specific_dir'):
> assert some_property(py_code)
>
> ?
>
I think a possible workaround is implementing your own
``pytest_generate_tests`` hook, see:
http://doc.pytest.org/en/latest/parametrize.html?highlight=pytest_generate_tests#basic-pytest-generate-tests-example
HTH,
Bruno.
_______________________________________________
pytest-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pytest-dev