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

Where reasonable, convert tests to free functions #993

Open
gadomski opened this issue Feb 15, 2023 · 3 comments
Open

Where reasonable, convert tests to free functions #993

gadomski opened this issue Feb 15, 2023 · 3 comments
Milestone

Comments

@gadomski
Copy link
Member

We switched to pytest in #939, which means we don't have to use unittest.TestCase anymore; we can switch to free functions. In most cases, converting tests should be as simple as changing:

class MyTestCase(unittest.TestCase):
    def test_foo(self) -> None:
        self.assertTrue(True)

to

def test_foo() -> None:
    assert True

While this is a little bike-sheddy, this does have some benefits:

  • Enable fixtures, parameterization, and custom hooks
  • Reduces confusion for new contributors, who might be unsure of which test format to use
  • Reduces the indentation level (a little thing, I know, but IMO it reads cleaner to have less leading whitespace)
github-merge-queue bot pushed a commit that referenced this issue Oct 16, 2023
* refactor: normalize import style

#1259

* refactor(tests): modernize version extension tests

- #993
- #948

* feat: add experimental to version ext

* feat: version is optional in version extension

* refactor: test import naming, ext

* feat: catalog extensions

* refactor: import style

* feat: add history media type

* feat: separate BaseVersionExtension

* feat: impl migration

* doc: fixups

* chore: update changelog

* fix(tests): mark some extra vcrs

* fix: pop_if_none on deprecated
@gadomski gadomski added this to the v2.0 milestone Sep 23, 2024
@teks
Copy link
Contributor

teks commented Jan 28, 2025

This seems straightforward so I'll make an attempt. 28 files have import unittest so I may split it into a few PRs so they don't get too long.

However, please let me know if there's a body of tests I should leave alone until some other work is done.

@gadomski
Copy link
Member Author

Nope, go to town 😄

@teks
Copy link
Contributor

teks commented Feb 18, 2025

Here are the sed invocations I'm using to replace the unittest asserts in case someone else ends up working on this:

sed -i '' -E 's/self.assertEqual\((.*), (.*)\)/assert \1 == \2/' $FILENAME
sed -i '' -E 's/self.assertNotEqual\((.*), (.*)\)/assert \1 != \2/' $FILENAME
sed -i '' -E 's/self.assertDictEqual\((.*), (.*)\)/assert \1 == \2/' $FILENAME
sed -i '' -E 's/self.assertListEqual\((.*), (.*)\)/assert \1 == \2/' $FILENAME
sed -i '' -E 's/self.assertTrue\((.*)\)/assert \1/' $FILENAME
sed -i '' -E 's/self.assertFalse\((.*)\)/assert not \1/' $FILENAME
sed -i '' -E 's/self.assertIs\((.*), (.*)\)/assert \1 is \2/' $FILENAME
sed -i '' -E 's/self.assertIsNot\((.*), (.*)\)/assert \1 is not \2/' $FILENAME
sed -i '' -E 's/self.assertIsNone\((.*)\)/assert \1 is None/' $FILENAME
sed -i '' -E 's/self.assertIsInstance\((.*), (.*)\)/assert isinstance(\1, \2)/' $FILENAME
sed -i '' -E 's/self.assertIn\((.*), (.*)\)/assert \1 in \2/' $FILENAME
sed -i '' -E 's/self.assertNotIn\((.*), (.*)\)/assert \1 not in \2/' $FILENAME

Note this is macos sed, and regexes an imperfect subsitute for a real parser, so one still needs to visually inspect the results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants