Skip to content

Refactor test cases to improve unit test quality - #3298

Merged
bdarnell merged 6 commits into
tornadoweb:masterfrom
freddiewanah:master
Jun 8, 2024
Merged

bdarnell merged 6 commits into
tornadoweb:masterfrom
freddiewanah:master

Conversation

@freddiewanah

Copy link
Copy Markdown
Contributor

Hello, first-time submitting PR here.

This PR improves the readability of the assert statements in some of the unit tests by using more expressive assert methods from the unittest module. These changes improve code quality (avoid test smells) by:

  • Making the assert statements more readable, i.e., assertIsNone and assertIsNotNone.

Happy to update more related issues in the test code if needed :)

@bdarnell

Copy link
Copy Markdown
Member

The assertIsNone and assertIsNotNone changes are good and I'd be happy to see those methods adopted wherever applicable. (The same is true in most cases for all of the new assertion methods that were introduced in python 3, although sometimes it's more subtle and it's not quite equivalent to switch)

For the changes in gen_test that use str.join, though, I prefer them as they are. It's easier to read the single string than the list of string literals.

@freddiewanah

Copy link
Copy Markdown
Contributor Author

Sorry for the late reply. I did more static analysis to the code base during the time and got time to refactor more test cases.
As requested, I've undone the changes in gen_test.py.

In addition, I fixed more inconsistencies in the code, e.g., using assertIn and assertIsNone instead of keep using assertTrue and assertFalse. I found that using more precise assert statement could save the execution time of each individual test methods and hence save the overall project cost during the CICD.

@freddiewanah

Copy link
Copy Markdown
Contributor Author

@bdarnell May I get some updates on this one?

@bdarnell

Copy link
Copy Markdown
Member

Thanks, all these changes look great. I'm not expecting to see any savings in execution time and CI costs, but the new assertion methods give much better error messages when they fail so this is a worthwhile change. We do have a few black formatting issues (make sure you're using the version specified in requirements.txt), but other than that the PR looks good to merge.

Signed-off-by: Han Wang <[email protected]>
@freddiewanah

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and I've pushed a new commit to fix the format issue. Sorry about the previous check failed.

@bdarnell

bdarnell commented Jun 8, 2024

Copy link
Copy Markdown
Member

Thanks!

@bdarnell
bdarnell merged commit 69e4d80 into tornadoweb:master Jun 8, 2024
bdarnell added a commit to bdarnell/tornado that referenced this pull request Jun 10, 2024
That PR arrived while our CI was broken and I
manually verified that the tests passed but didn't
run the linters. These changes are running in to
python/mypy#5088
bdarnell added a commit that referenced this pull request Jun 10, 2024
test: Fix some lint issues after #3298
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants