mardi 1 septembre 2015

phpunit tests execution time gets insanely slow after adding just one more test

I've just started writing unit tests for my applications (with Codeception based on PHPUnit) and am experiencing this weird issue. here is the situation:

I have two test classes, GalleryBehaviorTest and GalleryModelTest:

class GalleryModelTest extends TestCase
{
    use Specify;

    public function testLoadGalleryByHandleAndWebsite()
    {
        $this->specify('returns gallery if handle exist', function(){
            $gallery = new Gallery;
            $gallery->handle = 'homepage';
            $gallery->websiteId = 1;
            $gallery->save();
            expect(
                Gallery::loadByHandleAndWebsite('homepage', 1)->id
            )->equals($gallery->id);
        });
    }

    public function testCreateWithHandleForWebsite()
    {
        $this->specify('creates new gallery with given handle and returns it', function(){
            expect(
                Gallery::createWithHandleForWebsite('homepage', 1)->handle
            )->equals('homepage');
        });
    }

    protected function tearDown()
    {
        Gallery::deleteAll();
        parent::tearDown();
    }
}


class GalleryBehaviorTest extends TestCase
{
use Specify;

public function setUp()
{
    parent::setUp();
    Yii::$app->user->setActiveWebsiteById(1);
}

public function testGalleryCreationAfterModelInsert()
{
    $this->specify('a gallery is created for a model after insert', function(){
        $news = $this->setupAndSaveNews();
        expect('make sure news object is saved', $news->id)->notEmpty();
        expect($news->galleryId)->notEmpty();
    });
}

public function tearDown()
{
    News::deleteAll();
    Gallery::deleteAll();
    parent::tearDown();
}

private function setupAndSaveNews()
{
    $news = new News;
    $news->title = 'Title';
    $news->content = 'News Content';
    $news->createdAt = $news->publishedAt = $news->updatedAt = time();
    $news->save(false);
    return $news;
}
}

these tests took about 2 seconds to run (is this memory usage normal?):

Time: 2.14 seconds, Memory: 73.25Mb
OK (3 tests, 4 assertions)

but when I add just one simple test method to GalleryBehaviorTest class, execution time increases massively (and look at the memory use!):

Time: 2.35 minutes, Memory: 475.50Mb
OK (4 tests, 5 assertions)

and here is the new test method:

public function testGalleryBehaviorIsAttached()
{
    $this->specify('gallery behavior is attached to the model', function(){
        $news = new News;
        $this->assertInstanceOf(
            GalleryBehavior::className(),
            $news->getBehavior('gallery')
        );
    });
}

It's a simple test that does not even connect to database. it actually does not matter what test I add to suite, when it becomes 4 tests, it starts to act like this. then removing any of these 4 tests will make it run fast again, any method from 2 classes!

as I said I have no background experience in this matter, any help and suggestion is appreciated.

p.s. I'm using Yii2 framework but not sure if that causes the problem

Aucun commentaire:

Enregistrer un commentaire