python - test initialization of object and use it -
i have class , want test built in unittest module. in particular want test if can create intances without throwing errors , if can use them.
the problem creation of objects quite slow, can create object in setupclass
method , reuse them.
@classmethod def setupclass(cls): cls.obj = myclass(argument) def testconstruction(self): obj = myclass(argument) def test1(self): self.assertequal(self.obj.metohd1(), 1)
the point is
- i creating 2 times expensive object
- setup calles before testconstruction, cannot check failure inside
testconstruction
i happy example if there way set testconstruction
executed before other tests.
why not test both initialization , functionality in same test?
class mytestcase(testcase): def test_complicated_object(self): obj = myclass(argument) self.assertequal(obj.method(), 1)
alternatively, can have 1 test case object initialization, , 1 test case other tests. mean have create object twice, might acceptable tradeoff:
class creationtestcase(testcase): def test_complicated_object(self): obj = myclass(argument) class usagetestcase(testcase): @classmethod def setupclass(cls): cls.obj = myclass(argument) def test_complicated_object(self): self.assertequal(obj.method(), 1)
do note if methods mutate object, you're going trouble.
alternatively, can this, again, wouldn't recommend it
class mytestcase(testcase): _test_object = none @classmethod def _create_test_object(cls): if cls._test_object none: cls._test_object = myclass(argument) return cls._test_object def test_complicated_object(self): obj = self._create_test_object() self.assertequal(obj.method(), 1) def more_test(self): obj = self._create_test_object() # obj cached, unless creation failed
Comments
Post a Comment