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

Clean up import side effects in tests #6241

Merged
merged 9 commits into from
Sep 16, 2024
Merged

Conversation

cognifloyd
Copy link
Member

When pants runs tests via pytest, it runs each file separately to facilitate fine-grained caching of test results (that can be invalidated based on the inferred deps of that test file). This revealed a variety of areas where tests rely on import-time side effects that happen when previous tests ran. So, this cleans up those side effects so that each test file can run in isolation.

In some cases, I was able to move the side effects from import-time to setUpClass time which feels cleaner to me.

As I identified imports with side-effects, I reviewed other places they were imported to consistently import them early with a comment explaining the dependency on import side-effects.

These commits were extracted from my wip work in #6202.

Various tests were relying on the side effects of tests that nosetest
runs before they ran. These include:
- 3 in test_action_alias_utils.py::TestInjectImmutableParameters
- 2 in test_jinja_render_data_filters.py
- 1 in test_logging_middleware.py
- 9 in test_operators.py::SearchOperatorTest
- 3 in test_util_payload.py

In particular, the oslo config initialization from the tests in
st2common/tests/unit/services/ happens before these test ran, obscuring
their dependence on this initialization.

Pants runs each test file separately for fine-grained caching.
This looks like copy pasta as some of these files have a comment
saying that running this before importing something else is required.
However, by importing st2tests, that already implicitly happens
in st2tests/st2tests/base.py. Then tests_config.parse_args() gets called
again in the class init.

Plus, I reviewed all the other imports, and none of them have import time
side effects that matter for oslo config bits. So, these calls are not
necessary, and the comments about them are wrong.
pants runs each test file separately. test_workflow_rerun only worked
under nosetest because earlier files already did the monkey_patch.
Without this, running this file in isolation, with either nosetest
or pytest, hangs.
…ffects

importing anything form st2tests already handles running
st2tests.config.parse_args() on import before loading
the files from st2common that need those side effects.
So, rely on that, and on the db test case base classes
for running parse_args() where appropriate.

The import side-effects are unfortunate, but this reduces
how many places are making those changes.
…fects

importing anything form st2tests already handles running
st2tests.config.parse_args() on import before loading
the files from st2common that need those side effects.
So, rely on that, and on the db test case base classes
for running parse_args() where appropriate.

The import side-effects are unfortunate, but this reduces
how many places are making those changes.
@cognifloyd cognifloyd added this to the pants milestone Sep 16, 2024
@cognifloyd cognifloyd self-assigned this Sep 16, 2024
@pull-request-size pull-request-size bot added the size/L PR that changes 100-499 lines. Requires some effort to review. label Sep 16, 2024
@cognifloyd cognifloyd merged commit aa01b88 into master Sep 16, 2024
34 checks passed
@cognifloyd cognifloyd deleted the tests-import-side-effects branch September 16, 2024 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infrastructure: ci/cd pantsbuild size/L PR that changes 100-499 lines. Requires some effort to review. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants