-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
base: main
Are you sure you want to change the base?
1357 convert unittest to pytest #3931
Conversation
…ggurjar333/pudl into 1357-convert-unittest-to-pytest
For more information, see https://pre-commit.ci
…ggurjar333/pudl into 1357-convert-unittest-to-pytest
For more information, see https://pre-commit.ci
For more information, see https://pre-commit.ci
…ggurjar333/pudl into 1357-convert-unittest-to-pytest
@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 have installed pre-commit I think it is messing around here somewhere. |
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.
|
Explanation of the issue for the record here: Solution: |
There was a problem hiding this 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): |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.patch
s 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): |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.""" |
There was a problem hiding this comment.
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.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""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): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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!
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