Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

[1LP][RFR] Fix provider fixture scope #10063

Merged
merged 1 commit into from
Apr 22, 2020
Merged

[1LP][RFR] Fix provider fixture scope #10063

merged 1 commit into from
Apr 22, 2020

Conversation

tpapaioa
Copy link
Contributor

When the provider marker has scope='module' (or anything other than function) set, the provider fixture was not properly scoped. The fixture, and any other fixtures depending on it, would teardown and setup between each test. This PR fixes that behavior with the following changes:

1.) De-duplicate the DataProvider instances created during test collection / parametrization, so that the same instance is always used for the same combination of provider category, type, and version. This is implemented by using a new class method in DataProvider, get_instance(), which stores previously-created instances in a private class-level attribute, _instances. This is a dictionary where the key is the tuple (category, type, version), and the value is the corresponding instance. In a sample test collection run, this drastically cut down the number of DataProvider instances created from 24,605 to 19.

2.) Use the DataProvider instance as the provider fixture's cache key. Previously, the BaseProvider subclass instance was stored here, but this instance is created for a test only after pytest checks whether the cache key of the test's fixture request matches the cached result from the previous fixture execution. Because the DataProvider instances have been de-duplicated and are created during test collection, using them as the key will guarantee that this check succeeds.

Before this fix, a pytest run with the --setup-show option would show teardown/setup of module-scoped fixtures between tests in the same module. With the fix, lines like the following between tests will no longer appear:

    TEARDOWN M provider[vSphere 6.5 (nested)]
    SETUP          M provider[vSphere 6.5 (nested)]

{{ pytest: -v --setup-show -m smoke }}

…rk has scope='module':

1.) De-duplicate DataProvider instances during test collection so that the same instance is always
used for the same provider category / type / version.
2.) Use the DataProvider instance as the fixture's cache key.
 set.be module scoped when the provider marker
actually be module scoped when
@tpapaioa tpapaioa changed the title [WIPTEST] Fix module scoped provider fixture [RFR] Fix provider fixture scope Apr 19, 2020
Copy link
Contributor

@john-dupuy john-dupuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, awesome PR @tpapaioa!

@john-dupuy john-dupuy changed the title [RFR] Fix provider fixture scope [1LP][RFR] Fix provider fixture scope Apr 22, 2020
@mshriver mshriver merged commit cdd77b1 into ManageIQ:master Apr 22, 2020
@tpapaioa tpapaioa deleted the fix_provider_fixture_scope branch April 23, 2020 00:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants