Set fixtures for tests

I do not find in the docks how to set fixtures for tests. I.e. some test data for each table. I think it would be ideal to even have a separate test database, but I would like it to be filled in at the start of the tests and cleared at the end. So that if I changed something in it with tests , then when I restarted the tests, everything was as new. For example, in the root of the tests there is a folder _data, it seems that you need to shove fixtures into it? But in what format? And how to pick them up later? I would like of course, you can specify it as a simple array or in json, so that it is easy to change it if necessary. Still, you don't need much data for tests.

And another question I am very concerned about: how do I even perform something before and after all the tests (within a single file at least)? The tests have _before and _after methods, but they are executed before and after each test method, and I need before and after all test methods (at least within the same test class). For example the same fixtures don't make sense fill and clear before/after each method, and there are still different tasks.

Author: ПЭХАПЭШНИК, 2018-10-11

1 answers

Fixtures are an important part of testing. Their main task is to prepare an environment with a pre-fixed / known state to guarantee the repeatability of the testing process. Yii provides a framework that allows you to easily and accurately define fixtures and use them in your tests.

The key concept in the Yii fixture framework is the so-called fixture object. The fixture object represents a special aspect of the test object. the environment that inherits from yii \ test\Fixture or its heirs. For example, you can use UserFixture to make sure that the user table contains a known set of data. You load one or more fixture objects before running the test and unload them after the test is completed.

The fixture can depend on other fixtures set via the yii\test\Fixture property::$depends. When a fixture is loaded, the fixtures on which it depends will be automatically loaded BEFORE it, and when it is unloaded all dependent fixtures will be unloaded AFTER it. (https://www.yiiframework.com/doc/guide/2.0/ru/test-fixtures)

 0
Author: Дмитрий, 2018-10-22 07:13:41