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

1357 convert unittest to pytest #3931

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

ggurjar333
Copy link

@ggurjar333 ggurjar333 commented Oct 26, 2024

Overview

Closes #1357.

What problem does this address?

Replacing Unittest with Pytest to standardizing the testing code.

What did you change?

I have changed Unittest based code with Pytest

Documentation

Make sure to update relevant aspects of the documentation.

Tasks

Testing

How did you make sure this worked? How can a reviewer verify this?

To-do list

ggurjar333 and others added 20 commits October 19, 2024 21:03
@zaneselvans
Copy link
Member

@ggurjar333 I think you'll want to install the git pre-commit hooks locally so that all the static code analysis and formatting checks can be run before anything gets pushed. Some pointers in our docs here

@ggurjar333
Copy link
Author

ggurjar333 commented Nov 1, 2024

@ggurjar333 I think you'll want to install the git pre-commit hooks locally so that all the static code analysis and formatting checks can be run before anything gets pushed. Some pointers in our docs here

@zaneselvans
I'm facing these errors.
{150E6F8C-AF51-4A8C-B4B2-679322C02081}

I have installed pre-commit

{9939A25D-EE21-4667-9D7E-F49DB1D84C11}

I think it is messing around here somewhere.

{AA83BF4C-1313-41DC-9025-26B1D6199195}

I tried to delete cache this way as well.
{0B614920-353C-40D0-9F83-C98775765CFD}

How to fix this one?
{CF70B44F-383D-47FF-9E6C-310B3060A38F}

@zaneselvans
Copy link
Member

It looks like there are a bunch of genuine test failures on your branch, so I think we should try and separate the errors that are due to changes from the errors that are due to environment / setup issues.

  • Were you able to successfully run the unit tests before making any changes?
  • Check out the main branch and run make pytest-unit from the top-level of the repository.
  • What kind of errors come up then?
  • How did you create the pudl-dev conda environment?

@zaneselvans zaneselvans marked this pull request as draft November 1, 2024 15:59
@ggurjar333 ggurjar333 marked this pull request as ready for review November 2, 2024 06:21
@ggurjar333
Copy link
Author

It looks like there are a bunch of genuine test failures on your branch, so I think we should try and separate the errors that are due to changes from the errors that are due to environment / setup issues.

  • Were you able to successfully run the unit tests before making any changes?
  • Check out the main branch and run make pytest-unit from the top-level of the repository.
  • What kind of errors come up then?
  • How did you create the pudl-dev conda environment?

Explanation of the issue for the record here:
I am using WSL on Windows. The issue was I had mounted a volume from the Linux side, the disconnect may be what those two Windows vs Linux file paths difference, even if they were trying to actually pointing at the same files on disk. I was trying to commit from within PyCharm folder from linux (as the folder was on Windows) or some shell thing that PyCharm does it might see the Windows path, while pre-commit sees the Linux path.

Solution:
Zane helped me to understand and I moved the folder from Windows path to Linux path at the root directory. It Works!

@zaneselvans zaneselvans added the testing Writing tests, creating test data, automating testing, etc. label Nov 8, 2024
Copy link
Member

@jdangerx jdangerx left a comment

Choose a reason for hiding this comment

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

Overall this looks much nicer when we're consistently using pytest - thank you! I do think that there are some spots where we're using the legacy setup / teardown behavior - while we're here converting everything to the new style, I think we should finish the job and convert those into pytest fixtures.

@@ -16,16 +12,16 @@


class FakeExtractor(CsvExtractor):
def __init__(self):
def __init__(self, mocker):
Copy link
Member

Choose a reason for hiding this comment

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

Non-blocking issue - but I think the FakeExtractor should expect to be passed a ds and not the Pytest mocker fixture. No reason the FakeExtractor needs to know what class the datastore it's being passed is.

),
):
res = extractor.extract(**PARTITION)
mocker.patch.object(CsvExtractor, "load_source", return_value=df)
Copy link
Member

Choose a reason for hiding this comment

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

What was the reasoning here for pulling these mocker.patchs out of a context manager?

assert len(res) == 1 # Assert only one page extracted
assert list(res.keys()) == [PAGE] # Assert it is named correctly
assert (
res[PAGE][company_field][0] == company_data
) # Assert that column correctly renamed and data is there.


@patch.object(FakeExtractor, "METADATA")
def test_validate_exact_columns(mock_metadata, extractor):
def test_validate_exact_columns(mocker, extractor):
Copy link
Member

Choose a reason for hiding this comment

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

Cool! Thanks for cleaning up the @patch.object(FakeExtractor, "METADATA").

"""Tests basic operation of the excel.Metadata object."""

@pytest.fixture(autouse=True)
Copy link
Member

Choose a reason for hiding this comment

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

This works, but the recommended way of implementing these xunit-style setup methods is to use def setup_class: https://docs.pytest.org/en/7.1.x/how-to/xunit_setup.html#class-level-setup-teardown

But, the even more recommended thing is to just turn this into a normal fixture called metadata and refer to that everywhere we need it.

@@ -8,20 +6,20 @@


class FakeExtractor(Extractor):
def __init__(self):
def __init__(self, mocker):
Copy link
Member

Choose a reason for hiding this comment

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

See above non-blocking comment about passing in the actual dependencies vs. passing in mocker.

@@ -185,6 +189,7 @@ def setUp(self):
)

def test_leafward_prop_undecided_children(self):
"""Test leadward propagation with undecided children."""
Copy link
Member

Choose a reason for hiding this comment

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

Were pre-commit checks yelling at you for not adding docstrings to these test methods? If so we should tell the check to ignore test docstrings.

@@ -185,6 +189,7 @@ def setUp(self):
)

def test_leafward_prop_undecided_children(self):
"""Test leadward propagation with undecided children."""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""Test leadward propagation with undecided children."""
"""Test leafward propagation with undecided children."""

@@ -243,7 +209,8 @@ class TestZenodoFetcher(unittest.TestCase):
r"^10\.(5072|5281)/zenodo\.(\d+)$", PROD_EPACEMS_DOI
).group(2)

def setUp(self):
@pytest.fixture(autouse=True)
def setup(self):
Copy link
Member

Choose a reason for hiding this comment

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

Another flag for turning this into normal pytest fixtures.

"""Unit tests for the LocalFileCache class."""

def setUp(self):
def setup_method(self):
Copy link
Member

Choose a reason for hiding this comment

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

I think this setup/teardown should be turned into a fixture as well.

"""Unit tests for LayeredCache class."""

def setUp(self):
def setup_method(self):
Copy link
Member

Choose a reason for hiding this comment

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

Flagging another spot to fixture-ify!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Writing tests, creating test data, automating testing, etc.
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

Convert tests using unittest to pytest
3 participants