[pytest-dev] Garbage collection and pytest fixtures
Floris Bruynooghe
flub at devork.be
Sun Oct 8 16:03:55 EDT 2017
Hi Dan,
On 7 October 2017 at 22:32, Dan Nealschneider
<dan.nealschneider at gmail.com> wrote:
> If I have a fixture like the following, should I expect the object returned
> by the fixture to be garbage collected after the tests that use it complete?
> Or after all the tests in the module complete? Or after the seesion
> completes?
>
> @pytest.fixture(scope='module')
> def big_item():
> return [0] *1000000
>
> Is this a reasonable way to use a fixture, or is there a better way to reuse
> a large and
> expensive-to-create item across tests?
This is perfectly reasonable. The fixture should be torn down soon
after the last test uses it and then it should be up to garbage
collection to clean it up. In the worst case this would indeed stay
in memory for the whole module. If pytest somehow keeps references
after this that's probably a bug.
Note however that there is one exception to this. If any of the tests
with the fixture failed then the traceback object of that failed test
will stay in memory for pytest, so that it can use this later for
reporting etc. If this traceback somehow refers to the large fixture
then it will be kept alive. If this really is a problem then there's
probably some work that could be done, but it won't be trivial and
will need someone to dedicate some time to improve this. I think this
has been mentioned in the past however and in the end no one was hurt
that bad by it that they figured out how to improve this.
Kind regards,
Floris
More information about the pytest-dev
mailing list