I want to write unit tests for application models yii2 using codeception. I am just starting to get acquainted with testing and directly with a codeception, and for now I don’t understand this:
For all models there will be a fairly extensive set of identical tests, for example, various validation checks. I would like to avoid copy-paste and implement the concept of inheritance. Type to write one base class of the test for models and from it already to be inherited adding it with the necessary tests as necessary.
The difficulty is that while I can not figure out how to do it beautifully in the codeception.
So far only such idea came to mind:
Base class
abstract class AbstractModelTest extends \Codeception\Test\Unit { /** * @var \UnitTester */ protected $tester; protected function _before() { } protected function _after() { } protected function _testAttributeLabels() { //test code } protected function _testRequiredValidation() { //test code } } Class heir
class ModelTest extends \Codeception\Test\Unit { /** * @var \UnitTester */ protected $tester; protected function _before() { } protected function _after() { } public function testAttributeLabels() { $this->_testAttributeLabels(); } public function testRequiredValidation() { $this->_testRequiredValidation(); } } But it looks very crutch, surely it should be possible to realize it more elegantly.