django:用不同的固定装置重复测试用例
django: repeat testcase with different fixtures
我有一个 Django TestCase,我想 运行 对两组不同的数据(在本例中,颜色可以是红色或蓝色的 Bike 对象)进行所有测试。
无论是通过加载不同的灯具,还是通过相同的灯具和操纵颜色,我都没有偏好。
见下例:
class TestBike(TestCase):
fixtures = [
"testfiles/data/blue_bike.json",
]
def setUp(self, *args, **kwargs):
self.bike = Bike.objects.get(color="blue")
run_expensive_commands(self.bike)
def test_bike_1(self):
# one of many tests
def test_bike_2(self):
# second of many tests
我考虑过的一种方法是使用 parameterized
包,但我必须 parameterize
每个测试,并从每个测试调用 prepare()
函数。听起来有很多冗余。
另一个是用不同的fixture 多重继承测试。对我来说也有点太冗长了。
您是否考虑过 class 级别的参数化,即通过 class 属性?
class TestBikeBase:
COLOR = "" # Override in derived class.
def setUp(self, *args, **kwargs):
self.bike = Bike.objects.get(color=self.COLOR)
run_expensive_commands(self.bike)
def test_bike_1(self):
# one of many tests
def test_bike_2(self):
# second of many tests
class TestRedBike(TestBikeBase, TestCase):
COLOR = "red"
class TestBlueBike(TestBikeBase, TestCase):
COLOR = "blue"
我有一个 Django TestCase,我想 运行 对两组不同的数据(在本例中,颜色可以是红色或蓝色的 Bike 对象)进行所有测试。
无论是通过加载不同的灯具,还是通过相同的灯具和操纵颜色,我都没有偏好。
见下例:
class TestBike(TestCase):
fixtures = [
"testfiles/data/blue_bike.json",
]
def setUp(self, *args, **kwargs):
self.bike = Bike.objects.get(color="blue")
run_expensive_commands(self.bike)
def test_bike_1(self):
# one of many tests
def test_bike_2(self):
# second of many tests
我考虑过的一种方法是使用 parameterized
包,但我必须 parameterize
每个测试,并从每个测试调用 prepare()
函数。听起来有很多冗余。
另一个是用不同的fixture 多重继承测试。对我来说也有点太冗长了。
您是否考虑过 class 级别的参数化,即通过 class 属性?
class TestBikeBase:
COLOR = "" # Override in derived class.
def setUp(self, *args, **kwargs):
self.bike = Bike.objects.get(color=self.COLOR)
run_expensive_commands(self.bike)
def test_bike_1(self):
# one of many tests
def test_bike_2(self):
# second of many tests
class TestRedBike(TestBikeBase, TestCase):
COLOR = "red"
class TestBlueBike(TestBikeBase, TestCase):
COLOR = "blue"