Suppose you are writing unit tests with the unittest framework within Python.
I am used to writing unit tests using classes and various methods for each test e.g. if you had some function cuboid_volume that calculates the volume of a cube:
def cuboid_volume(l):
return (l*l*l)
Then you would structure units tests as follows:
import unittest
class TestCuboid(unittest.TestCase):
def test_volume(self):
self.assertAlmostEqual(cuboid_volume(2),8)
However I have also seen examples where unit tests are structured without using a class. Example below:
def test_volume():
assert cuboid_volume(2) == 8
My question is: what is the benefit of doing this and why use a class in the first place?
benefit to use class
unittest.TestCase, that is why you can use self.assertAlmostEqualsetUp and tearDown called per test method, or setUpClass and tearDownClass per class.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With