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

Generate lockfile for using pytest #5838

Merged
merged 3 commits into from
Dec 8, 2022
Merged

Generate lockfile for using pytest #5838

merged 3 commits into from
Dec 8, 2022

Conversation

cognifloyd
Copy link
Member

Background

This is another part of introducing pants, as discussed in various TSC meetings.

Related PRs can be found in:

Overview of this PR

This configures pants so it can use pytest when running the test goal. However, our test suite does not completely support pytest. Getting this lockfile in is a prerequisite to being able to running tests with it.

pytest version

I opted to go with the version of pytest that pants has pinned by default.

st2/pants.toml

Lines 147 to 149 in 43e2a1d

[pytest]
lockfile = "lockfiles/pytest.lock"
version = "pytest==7.0.1" # copied from https://www.pantsbuild.org/v2.14/docs/reference-pytest#version

pytest plugins

By default pants includes pytest-cov and pytest-xdist plugins for pytest. We get those too because I'm using extra_requirements.add; if I used extra_requirements it would overwrite those.

st2/pants.toml

Line 150 in 43e2a1d

extra_requirements.add = [

We already install pytest-benchmark[histogram] for our tests in st2common/benchmarks, so I copied that pinned version as is:

pytest-benchmark[histogram]==3.4.1

st2/pants.toml

Line 151 in 43e2a1d

"pytest-benchmark[histogram]==3.4.1", # used for st2common/benchmarks

Then I added several other plugins, some of which I left commented (we can check them out later when we do more with pytest):

st2/pants.toml

Lines 152 to 158 in 43e2a1d

#"pytest-timer[colorama]", # report test timing (--with-timer ala nose-timer)
"pytest-icdiff", # make diff output easier to read
"pygments", # highlight code in tracebacks
# other possible plugins
#"pytest-timeout", # time limit on tests
#"pytest-mock", # more convenient mocking

This is partially based on recommendations in the pants docs. See: https://www.pantsbuild.org/docs/python-test-goal
These plugins have made what I've done with pytest so far, more enjoyable.

pytest args

As recommended at https://www.pantsbuild.org/docs/python-test-goal#controlling-output, I dropped the pytest header. Pants runs each test file separately to help with caching, so that header becomes very annoying and repetitive.

st2/pants.toml

Lines 164 to 166 in 43e2a1d

args = [
"--no-header", # don't print pytest version for every tested file
]

parallel tests with mongo

This line of the config builds on #5707, so that pants can safely run tests that use mongo in parallel.

st2/pants.toml

Line 167 in 43e2a1d

execution_slot_var = "ST2TESTS_PARALLEL_SLOT"

see:

@cognifloyd cognifloyd added this to the pants milestone Dec 7, 2022
@cognifloyd cognifloyd self-assigned this Dec 7, 2022
@pull-request-size pull-request-size bot added the size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review. label Dec 7, 2022
Comment on lines +161 to +162
#"pytest-cov", # coverage
#"pytest-xdist", # parallel test runs (pants uses this if [pytest].xdist_enabled)

Choose a reason for hiding this comment

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

You won't be able to use some Pants features with these commented out

Copy link
Member Author

Choose a reason for hiding this comment

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

This serves as documentation because I'm using extra_requirements.add

@cognifloyd cognifloyd enabled auto-merge (squash) December 8, 2022 17:43
@cognifloyd cognifloyd merged commit cc737a8 into master Dec 8, 2022
@cognifloyd cognifloyd deleted the pants-pytest branch December 8, 2022 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external dependency pantsbuild size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants