N
N
nepster-web2014-03-02 15:59:28
Yii
nepster-web, 2014-03-02 15:59:28

How to write tests in Yii?

For the second day I have been trying to figure out how to write tests in Yii and nothing comes of it.
According to this manual
php-md5.blogspot.com/2013/05/phpunit-selenium-rc-y...
I installed the whole thing with phpunit
Go to the folder:
\yii2\apps\advanced\frontend\tests\unit
Create my own first test MyOneTest.php :

use \yii\codeception\TestCase;

class MyOneTest extends TestCase
{
    public function testFailure()
    {
        $this->assertTrue(true);
    }
}

I also picked up an example from the manuals somewhere, I run: phpunit MyOneTest.php
F:\OpenServer\domains\portf.ru\yii2\apps\advanced\frontend\tests\unit>phpunit My
OneTest.php
PHP Fatal error:  Class 'yii\codeception\TestCase' not found in F:\OpenServer\do
mains\portf.ru\yii2\apps\advanced\frontend\tests\unit\MyOneTest.php on line 6

Fatal error: Class 'yii\codeception\TestCase' not found in F:\OpenServer\domains
\portf.ru\yii2\apps\advanced\frontend\tests\unit\MyOneTest.php on line 6

Wrote something like this:
class MyOneTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertTrue(true);
    }
}

Everything seems to be working.
PHPUnit 3.7.32 by Sebastian Bergmann.

.

Time: 72 ms, Memory: 1.75Mb

OK (1 test, 1 assertion)

But in this case, as I understand it, I will not have access to the Yii components in the test methods. Please tell me what to do?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-03-02
Protko @Fesor

well, you can't find the class \yii\codeception\TestCase. You probably didn't connect the YIi autoloader.

G
gro, 2014-03-03
@gro

I don't know about Yii.
phpunit has a config - phpunit.de/manual/current/en/appendixes.configurat...
There you can specify bootstrap, a file that will be included when phpunit starts.
In it, apparently, you need to register autoload.

M
Maxim, 2015-09-15
@go3l337

Everything works out of the box, you need to run it from the tests folder, there is a bootstrap file that loads all the necessary classes.
In this case, the command will be something like this:
Or all tests from the unit folder
phpunit --verbose unit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question