Python testing best practice

Code Review Doctor suggests a fix if your code does not follows Python testing best practices.

This gives you greater confidence in the quality of your tests because it is not what you test that is important, but how you test.

Money
code-review-doctorbotsuggested changes just now
tests.py
1
+
class TestFeature(unittest.TestCase):
2
+
    def test_feature(self):
3
+
        self.assertEquals(int('1'), 1)
Suggested changes
-
        self.assertEquals(int('1'), 1)
+
        self.assertEqual(int('1'), 1)
Commit suggestion

Dont use deprecated TestCase assertions like self.assertEquals. Use their modern counterpart instead like self.assertEqual.

Read more
tests.py
1
+
class TestFeature(unittest.TestCase):
2
+
    def test_feature(self):
3
+
        self.assertTrue(value_one, value_two)
Suggested changes
-
        self.assertTrue(value_one, value_two)
+
        self.assertEqual(value_one, value_two)
Commit suggestion

assertTrue is not for comparing arguments. Consider assertEqual or others instead.

Read more
tests.py
1
+
class TestFeature(unittest.TestCase):
2
+
    def test_feature(self):
3
+
        self.assertTrue(value in values)
Suggested changes
-
        self.assertTrue(value in values)
+
        self.assertIn(value, values)
Commit suggestion

assertIn and assertNotIn provide more helpful failure messages than assertTrue or assertFalse.

Read more
tests.py
1
+
class TestFeature(unittest.TestCase):
2
+
    def test_feature(self):
3
+
        self.assertTrue(value is not None)
Suggested changes
-
        self.assertTrue(value is not None)
+
        self.assertIsNotNone(value)
Commit suggestion

assertIsNone and assertIsNotNone provide more helpful failure messages than assertTrue or assertFalse.

Read more
Update tests.py

Are you ready to improve your team agility through lower tech debt? Add Code Review Doctor to GitHub.