Modulok wrote:
List,

When you create unit tests, do you group tests for a given function
together into one unit test method, or do you prefer to have separate
test methods for every assert statement?

Each test method should test one thing. That doesn't necessarily mean one assert, but one conceptual thing. Here's an example from one of my test suites:


class QuartileSkewnessTest(unittest.TestCase):
    def testFailure(self):
        # Test that function correctly raises an exception if the
        # arguments are out of order.
        self.assertRaises(ValueError, stats.quartile_skewness, 2, 3, 1)
        self.assertRaises(ValueError, stats.quartile_skewness, 9, 8, 7)

    def testNan(self):
        # Test that the degenerate case where all three arguments are
        # equal returns NAN.
        self.assertTrue(math.isnan(stats.quartile_skewness(1, 1, 1)))
        self.assertTrue(math.isnan(stats.quartile_skewness(5, 5, 5)))

    def testSkew(self):
        # Test skew calculations.
        self.assertEqual(stats.quartile_skewness(3, 5, 7), 0.0)
        self.assertEqual(stats.quartile_skewness(0, 1, 10), 0.8)
        self.assertEqual(stats.quartile_skewness(0, 9, 10), -0.8)


In this particular case, I happen to have one test class for this function, but that's not always the case. Each test class is for a set of related tests (e.g. "test everything about this function", or "test that this same property holds for all eight of these functions", or similar). Within the test class, each test method is for one test, not necessarily one assert.


--
Steven

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to