Basic test class for python unittest

I have a bunch of objects that "implement" the same "interface". There are several common actions that they all must take, and that I therefore want to test in all of these classes.

I thought about the fact that I have a basic test case with general material, which is then obtained from certain test cases. There is at least one abstract method in this base test class that is called from the base class itself and must be defined in derivatives. I'm not sure what to call the method defined in the derivative of the base class. If I do self.abstract_method, it will say that the method is not defined. What can I call this method?

If there is a better way to achieve my testing goals here, which does not imply inheritance, I would be happy to hear about it.

+4
source share
2 answers

Using inheritance

import cPickle import pickle import unittest class TestPickleBase: def test_dumps_loads(self): self.assertEqual(self.pickle_impl.loads(self.pickle_impl.dumps('a')), 'a') class TestCPickle(unittest.TestCase, TestPickleBase): pickle_impl = cPickle class TestPickle(unittest.TestCase, TestPickleBase): pickle_impl = pickle if __name__ == '__main__': unittest.main() 

Using pytest

 import cPickle import pickle import pytest @pytest.mark.parametrize('pickle_impl', [pickle, cPickle]) def test_dumps_loads(pickle_impl): assert pickle_impl.loads(pickle_impl.dumps('a')) == 'a' 

 import cPickle import pickle import pytest @pytest.fixture(params=[pickle, cPickle]) def pickle_impl(request): return request.param def test_dumps_loads(pickle_impl): assert pickle_impl.loads(pickle_impl.dumps('a')) == 'a' 
+3
source

You can define an empty version of a method in your abstract classes, not to say that the method is not defined like this:

 class AbstractClass(object): def abstract_method(self): pass class ConcreteClass(AbstractClass): def abstract_method(self): # Insert logic here. 
+1
source

Source: https://habr.com/ru/post/1496376/


All Articles