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

Split up unit test files and rename tests directory to test #351

Merged
merged 8 commits into from
Jun 21, 2022

Conversation

algochoi
Copy link
Contributor

@algochoi algochoi commented Jun 16, 2022

This PR separates the different unit test classes into different files for improved readability.

This PR also proposes to rename the test directory to tests directory to prevent any conflicts with the existing Python test package.

Refactoring

Revert and refactor unit tests

Finish moving tests to separate modules

Remove test feature files

Change paths for tests

Change makefile

Finish moving tests to separate modules

Remove test feature files
@algochoi algochoi changed the title Split up test_unit.py files and rename tests directory to test Split up unit test files and rename tests directory to test Jun 16, 2022
@algochoi algochoi marked this pull request as ready for review June 16, 2022 20:14
test_unit.py Outdated
[m.get_signature() for m in c.methods],
["add(uint64,uint64)void", "multiply(uint64,uint64)void"],
)


if __name__ == "__main__":
Copy link
Contributor

@tzaffi tzaffi Jun 16, 2022

Choose a reason for hiding this comment

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

I see we are keeping this file to act as a "driver" that determines which tests to actually run. I think this is a good opportunity to move away from this approach. For example, last week I almost took a victory lap on my encoder unit test before I realized that it wasn't getting run at all (I had it add it on the former line 4160 of this file). I don't see a compelling reason to have tests in the codebase that aren't being run. I believe that with your new refactoring, all tests will be run if we change line 28 of config.yml to the following (but haven't actually tried it) assuming you want to:

  • run these unit tests in an asynchronous fashion and have added pytest-xdist==2.5.0 to requirements.txt
  • print the 10 slowest tests at the end of each run
pytest -n auto --durations=10 -sv tests/unit_tests

Copy link
Contributor

Choose a reason for hiding this comment

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

For those with more Python experience: If we move away from requiring a driver to run the tests, is it more idiomatic to co-locate non-cucumber unit test files with source files (named as <test_name>_test.py rather than the separate dir?

From my scan of https://docs.python.org/3/library/unittest.html#test-discovery, it seems _test.py is an accepted convention.

Copy link
Contributor

Choose a reason for hiding this comment

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

I personally prefer a separate tests directory, but we seem to be mostly favoring the approach you mentioned. pytest is smart enough to figure it out if you give it the root directory.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it - alright, disregard my co-location question. Removing the driver seems prudent.

Copy link
Contributor

Choose a reason for hiding this comment

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

That being said, I must confess again to not actually trying it... I commit to trying this by standup if no one else has.

Copy link
Contributor

Choose a reason for hiding this comment

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

So is everyone in favor of removing the driver file and using something like: pytest tests/unit_tests?

On my side, yes. It feels less error prone because tests won't require registration.

I can also change the test file names to *_tests.py to follow the convention.

  • I don't feel strongly here. I also don't know what's considered common convention. I originally raised it when considering co-location with source files.
  • If it's considered Pythonic and you're up for the change, then I'm all for it.

Copy link
Contributor

Choose a reason for hiding this comment

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

(voting for removal)

Copy link
Contributor

Choose a reason for hiding this comment

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

So is everyone in favor of removing the driver file and using something like: pytest tests/unit_tests?

+1 to getting rid of test_unit.py. I think because our unit tests are written using unittest, not pytest, you may need to use an approach like this: https://stackoverflow.com/a/15630454, but regardless, auto discovery of all tests should be preferred over a driver file.

I can also change the test file names to *_tests.py to follow the convention.

I think it only makes sense to add a suffix like this when tests are co-located with other source files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, it looks like both of these commands will work:

$ python -m unittest discover tests/unit_tests

with pytest (better formatting):

$ pytest tests/unit_tests

Pytest seems to work with the version that's currently in the requirements.txt and outputs better formatting so I'd be inclined to use that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok great, I agree pytest should be preferred

Copy link
Contributor

@tzaffi tzaffi left a comment

Choose a reason for hiding this comment

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

A couple of minor suggestions and a bigger one to see if we can remove test_unit.py and run the unit tests asynchronously.

Copy link
Contributor

@michaeldiamant michaeldiamant left a comment

Choose a reason for hiding this comment

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

@algochoi Thanks for splitting out the tests. ☕

I don't consider myself an authoritative reviewer. Appreciate if you get at least 1 more approval prior to merging.

Copy link
Contributor

@tzaffi tzaffi left a comment

Choose a reason for hiding this comment

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

LGTM

My earlier suggestion to multi-thread the unit tests seems very low priority as they run so fast currently (especially in comparison with the integration tests)

@algochoi algochoi merged commit 100a458 into develop Jun 21, 2022
@algochoi algochoi deleted the refactor-unit-tests branch June 21, 2022 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants