Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python properties on test classes are executed during test collection #2234

Closed
3 tasks
bluetech opened this issue Feb 5, 2017 · 0 comments
Closed
3 tasks

Comments

@bluetech
Copy link
Member

bluetech commented Feb 5, 2017

  • Include a detailed description of the bug or suggestion

A test suite with a test file such as the following fails during test collection:

class TestIt:
    @property
    def prop(self):
        raise NotImplementedError()

I expect the property (or other types of custom descriptors) to not be executed during collection.

For comparison, unittest, from which I am migrating, does not execute properties (unless the tests use them of course).

  • pip list of the virtual environment you are using
Package    Version
---------- -------
pip        9.0.1  
py         1.4.32 
pytest     3.0.6  
setuptools 28.8.0 
  • pytest and operating system versions
$ uname -a
Linux 4.9.6-1-ARCH #1 SMP PREEMPT Thu Jan 26 09:22:26 CET 2017 x86_64 GNU/Linux
$ python --version
Python 3.6.0
bluetech added a commit to bluetech/pytest that referenced this issue Feb 5, 2017
Previously, properties such as in the added test case were triggered
during collection, possibly executing unintended code. Let's skip them
instead.

We should probably skip all custom descriptors, however I am not sure
how to do this cleanly, so for now just skip properties.

Fixes pytest-dev#2234.
bluetech added a commit to bluetech/pytest that referenced this issue Feb 6, 2017
Previously, properties such as in the added test case were triggered
during collection, possibly executing unintended code. Let's skip them
instead.

We should probably skip all custom descriptors, however I am not sure
how to do this cleanly, so for now just skip properties.

Fixes pytest-dev#2234.
bluetech added a commit to bluetech/pytest that referenced this issue Feb 7, 2017
Descriptors (e.g. properties) such as in the added test case are
triggered during collection, executing arbitrary code which can raise.
Previously, such exceptions were propagated and failed the collection.
Now these exceptions are caught and the corresponding attributes are
silently ignored.

A better solution would be to completely skip access to all custom
descriptors, such that the offending code doesn't even trigger. However
I think this requires manually going through the instance and all of its
MRO for each and every attribute checking if it might be a proper
fixture before accessing it. So I took the easy route here.

In other words, putting something like this in your test class is still
a bad idea...:

    @Property
    def innocent(self):
        os.system('rm -rf /')

Fixes pytest-dev#2234.
This was referenced Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant