Skip to content

Move reused data to pytest fixtures in test_grdtrack.py - #1353

Merged
seisman merged 6 commits into
masterfrom
test-grdtrack-dataframe-loading
Jun 25, 2021
Merged

seisman merged 6 commits into
masterfrom
test-grdtrack-dataframe-loading

Conversation

@willschlitzer

Copy link
Copy Markdown
Contributor

Five test functions in test_grdtrack.py use a dataframe from load_ocean_ridge_points() and called it inside the test functions. This pull requests moves it to fixture_dataframe().

Over five runs of test_grdtrack.py on my machine, the average time for the old tests was 906 ms and the new tests averaged 658 ms.

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@willschlitzer willschlitzer added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog labels Jun 23, 2021
@willschlitzer willschlitzer added this to the 0.5.0 milestone Jun 23, 2021
@maxrjones

Copy link
Copy Markdown
Member

Thanks, @willschlitzer! I would be interested in whether adding the csvfile and ncfile as fixtures speeds it up even more (used 3 times each):

@pytest.fixture(scope="module", name="csvfile")
def fixture_csvfile():
    """
    Load the grid data from the sample earth_relief file.
    """
    return which("@ridge.txt", download="c")


@pytest.fixture(scope="module", name="ncfile")
def fixture_ncfile():
    """
    Load the grid data from the sample earth_relief file.
    """
    return which("@earth_relief_01d", download="a")

@willschlitzer

Copy link
Copy Markdown
Contributor Author

Thanks, @willschlitzer! I would be interested in whether adding the csvfile and ncfile as fixtures speeds it up even more (used 3 times each):

Big time savings! 667 ms vs 411 ms, so with all of the fixtures this cuts down the test time by half!

Side note: Is there any reason we use @pytest.fixture instead of creating global variables in the script?

@seisman seisman added the final review call This PR requires final review and approval from a second reviewer label Jun 24, 2021

@weiji14 weiji14 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @willschlitzer!

Side note: Is there any reason we use @pytest.fixture instead of creating global variables in the script?

Good question. I think pytest fixtures (https://docs.pytest.org/en/latest/explanation/fixtures.html) are more explicit than global variables. Other than that, either fixtures or global variables would technically work for test_grdtrack.py. However, if we're going to parallelize tests (using something like pytest-xdist) at some point, then fixtures would probably be preferrable.

@seisman

seisman commented Jun 24, 2021

Copy link
Copy Markdown
Member

Good question. I think pytest fixtures (docs.pytest.org/en/latest/explanation/fixtures.html) are more explicit than global variables. Other than that, either fixtures or global variables would technically work for test_grdtrack.py. However, if we're going to parallelize tests (using something like pytest-xdist) at some point, then fixtures would probably be preferrable.

I'm wondering if there are global pytest fixtures. After a quick search, I find this (https://betterprogramming.pub/understand-5-scopes-of-pytest-fixtures-1b607b5c19ed). It seems a conftest.py file is what we may need, but I don't read the full post.

@weiji14

weiji14 commented Jun 24, 2021

Copy link
Copy Markdown
Member

Good question. I think pytest fixtures (docs.pytest.org/en/latest/explanation/fixtures.html) are more explicit than global variables. Other than that, either fixtures or global variables would technically work for test_grdtrack.py. However, if we're going to parallelize tests (using something like pytest-xdist) at some point, then fixtures would probably be preferrable.

I'm wondering if there are global pytest fixtures. After a quick search, I find this (https://betterprogramming.pub/understand-5-scopes-of-pytest-fixtures-1b607b5c19ed). It seems a conftest.py file is what we may need, but I don't read the full post.

Yes there are session, function, class, module and package scoped fixtures (see https://docs.pytest.org/en/latest/reference/fixtures.html#reference-fixtures). I'd prefer to keep the fixtures used by a test in the same test_*.py file and not to use a conftest.py file, because this makes it clearer where the fixtures come from (especially to new contributors unfamiliar with pytest).

@seisman

seisman commented Jun 25, 2021

Copy link
Copy Markdown
Member

I'd prefer to keep the fixtures used by a test in the same test_*.py file and not to use a conftest.py file, because this makes it clearer where the fixtures come from (especially to new contributors unfamiliar with pytest).

Yes, I agree.

When I worked on the PR #1359, I felt that the variable gmt_version should be a global variable or a global fixture, so that we don't have to add the following lines when we want to access the GMT library version.

from packaging.version import Version
from pygmt import clib
with clib.Session() as _lib:
    gmt_version = Version(_lib.info["version"])

@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Jun 25, 2021
@seisman seisman changed the title Move reused dataframe to pytest fixture in test_grdtrack.py Move reused data to pytest fixtures in test_grdtrack.py Jun 25, 2021
@seisman
seisman merged commit b689308 into master Jun 25, 2021
@seisman
seisman deleted the test-grdtrack-dataframe-loading branch June 25, 2021 18:18
@seisman seisman removed this from the 0.5.0 milestone Jul 25, 2021
@seisman seisman added this to the 0.4.1 milestone Jul 25, 2021

This branch was previously deployed

1 inactive deployment
Preview — 6ec1c400 Deployed Jun 25, 2021 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants