Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persist variable changes between tests in unittest?

How do I persist changes made within the same object inheriting from TestCase in unitttest?

from unittest import TestCase, main as unittest_main   class TestSimpleFoo(TestCase):     foo = 'bar'      def setUp(self):         pass      def test_a(self):         self.assertEqual(self.foo, 'bar')         self.foo = 'can'      def test_f(self):         self.assertEqual(self.foo, 'can')   if __name__ == '__main__':     unittest_main() 

I.e.: I want those two tests above to pass

like image 306
A T Avatar asked Jan 30 '14 04:01

A T


People also ask

Do unit tests need Docstrings?

This should be done through both doctests and standard unit tests using pytest. All public functions, classes, methods, etc. must have a docstring that follows the numpydoc conventions. Docstring tests should be short, easy-to-read tests that are instructive to a user.

Is PyUnit the same as unittest?

Yes. unittest is a xUnit style frameworkfor Python, it was previously called PyUnit.

Why pytest is better than unittest?

Pytest has rich inbuilt features which require less piece of code compared to unittest. In the case of unittest, we have to import a module, create a class and then define testing functions inside the class. But in the case of pytest, we have to define the functions and assert the conditions inside them.

Which function in unittest will run all of your tests?

TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.


1 Answers

As some comments have echoed, structuring your tests in this manner is probably a design flaw in the tests themselves and you should consider restructuring them. However, if you want to do this and rely on the fact that the test runner you are using executes them in an alphabetical (seemingly) order then I suggest the following.

Similar to what @Matthias was saying but I would do one thing differently for the cases where you may decide to inherit from the class at a later date.

from unittest import TestCase, main as unittest_main   class TestSimpleFoo(TestCase):     foo = 'bar'      def setUp(self):         pass      def test_a(self):         self.assertEqual(self.__class__.foo, 'bar')         self.__class__.foo = 'can'      def test_f(self):         self.assertEqual(self.__class__.foo, 'can')   if __name__ == '__main__':     unittest_main() 

The difference between this answer and @Matthias's answer you accepted is the explicit declaration of the class versus the lookup of said class reference.

TestSimpleFoo vs self.__class__ 

I prefer the dynamicness so I can inherit the tests later and run both test classes back to back and not have any cross over between the two. Because if you would choose to inherit from this class, explicitly naming the class reference would cause both test classes to run against that reference rather than their own respective classes.

like image 148
rdp Avatar answered Sep 22 '22 03:09

rdp