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

Conversation

ggurjar333
Copy link
Contributor

@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

Preview Give feedback

Testing

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

To-do list

Preview Give feedback

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
Contributor 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
Contributor 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 requested a review from jdangerx November 8, 2024 18:45
@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.

test/unit/extract/csv_test.py Outdated Show resolved Hide resolved
test/unit/extract/csv_test.py Outdated Show resolved Hide resolved
test/unit/extract/csv_test.py Show resolved Hide resolved
test/unit/extract/excel_test.py Outdated Show resolved Hide resolved
test/unit/extract/phmsagas_test.py Outdated Show resolved Hide resolved
test/unit/output/ferc1_test.py Outdated Show resolved Hide resolved
test/unit/output/ferc1_test.py Outdated Show resolved Hide resolved
test/unit/workspace/datastore_test.py Outdated Show resolved Hide resolved
test/unit/workspace/resource_cache_test.py Outdated Show resolved Hide resolved
test/unit/workspace/resource_cache_test.py Outdated Show resolved Hide resolved
@ggurjar333 ggurjar333 requested a review from jdangerx November 22, 2024 12:02
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.

Mostly looks good, I think we should revert back to the xunit-style setup for the FERC Form 1 forest test. Everything else is non-blocking. Thanks for working on this!

test/unit/extract/csv_test.py Outdated Show resolved Hide resolved
test/unit/extract/excel_test.py Outdated Show resolved Hide resolved
@@ -37,7 +36,104 @@
logger = logging.getLogger(__name__)


class TestForestSetup(unittest.TestCase):
@pytest.fixture
def forest_setup():
Copy link
Member

Choose a reason for hiding this comment

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

Pulling out just the nodes as a fixture doesn't help much, as you see you still have to unpack and add edges etc. The actual setup that has to get done for each test is "make a forest, with nodes, edges, and tags" - and each test has a slight variation on what those are. To me that means we actually want a helper function in this module that makes creating these forests a little less verbose, rather than a few fixtures.

But that involves a fair amount of restructuring of the tests, etc., and this PR has already gone on for a long time. I would just return to the xunit-style setup using the setup_method flow you use below.

Copy link
Member

Choose a reason for hiding this comment

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

@ggurjar333 I think this is the main thing I'd like to see before we merge the PR:

  1. undoing the structural change where you pull the nodes into a fixture
  2. converting the TestPrunedNode.setUp function into pytest using the classic xunit-style setup guide

You had mentioned wanting to spend more time learning about nx.DiGraphs - once you feel more comfortable with those you can open up another PR that just refactors ferc1_test.py to have a clearer/more flexible way of setting up graphs for testing.

@jdangerx
Copy link
Member

I went ahead and made the small change I requested in #4014 . Thanks for doing all this work @ggurjar333 !

@jdangerx jdangerx closed this Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community testing Writing tests, creating test data, automating testing, etc.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Convert tests using unittest to pytest
3 participants