开发者

How to run the same test for different class?

开发者 https://www.devze.com 2023-03-24 07:29 出处:网络
I have a test (unittest.TestCase) and I want to test more different implementation of a class with the same interfa开发者_StackOverflow社区ce.

I have a test (unittest.TestCase) and I want to test more different implementation of a class with the same interfa开发者_StackOverflow社区ce.

The max that I have do it's something like this:

def create_test(class_to_test):
    class TestClass(unittest.TestCase):
        def setUp(self):
            self.class_to_test = class_to_test
            self.class_to_test.two = 2
        def test_2(self):
            self.assertEqual(self.claa_to_test.two, 2)

    return TestClass

test = [create_test(x) for x in classes_to_test]

But I want just a bit more, I want that the definition of the class is in a different file and not into the function, and that the test was more standard as possible (the test should be "normal" as another one normal test).

I've just seen How to run the same test-case for different classes?...

EDIT: I know the name of the class to test only at run-time... (it'a a important particular)

I'm italian so my english isn't perfect, sorry.


Not entirely sure if this is what you're looking for, but you can subclass unittests:

class A:
   def __init__(self):
      self.a = 10

class B(A):
   def __init__(self):
      A.__init__(self)
      self.b = 50

class A_Test(unittest.TestCase):
   def setUp(self):
      self.test_obj = A()

   def test_A(self):
      self.assertEqual(self.test_obj.a, 10)

class B_Test(A_Test):
   def setUp(self):
      self.test_obj = B()

   def test_B(self):
      self.assertEqual(self.test_obj.b, 50)

test_A is inherited in B_Test, so this test will be run within the class on your (A-inherited) B object.

0

精彩评论

暂无评论...
验证码 换一张
取 消