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)

Don't use deprecated TestCase assertions like self.assertEquals. Use their modern counterpart instead like self.assertEqual.

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

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

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

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

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

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

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

Duplicate names for tests results in some tests being skipped.

Read more
Suggested changes
-
    def test_feature(self):
+
    def test_feature_two(self):
Commit suggestion
6
+
        ...
Update tests.py

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