Yii2:用于特定功能测试的夹具 [英] Yii2: loading fixtures for specific functional tests

查看:90
本文介绍了Yii2:用于特定功能测试的夹具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:使用Yii2高级应用程序模板和Codeception功能测试,是否有办法仅在特定场景(Cest类)之前加载特定灯具?

Short version: Using Yii2 Advanced Application Template and Codeception functional tests, is there a way to load a specific fixture only before a particular scenario (Cest class)?

(背景/旁注:我有一种模糊的感觉,我可能正在解决这个错误,因为可以说,如果测试应该针对db进行完全隔离,那么它们应该是单元测试,而不是功能测试。但是,由于时间限制,我在推迟单元测试的同时就开始进行功能测试的项目。我正在测试REST API,并且有一个案例严重依赖于测试数据,并且在其他测试修改了数据后失败了。)

(Background / side note: I have a vague feeling I might be approaching this wrong, since arguably, if tests should be completely isolated with regards to db, they should be unit tests and not functional ones. However, due to time constraints on the project I started with functional tests while postponing unit testing. I am testing a REST API and have a case which is strongly dependent on the testing data, and fails when the data is modified by other tests.)

我发现的一种方法是编写一个新的 _support\ApiHelper 类,以扩展默认的 FixtureHelper ,然后将 loadFixtures()/ unloadFixtures()调用放入 _before 而不是 _beforeSuite( )。但是,这会使所有灯具都在每次测试之前加载,这使得套件的执行非常缓慢。

One approach I found is to write a new _support\ApiHelper class extending the default FixtureHelper and put the loadFixtures()/unloadFixtures() calls in _before instead of _beforeSuite(). However, this makes all fixtures load before every test, which makes the suite's execution very slow.

推荐答案

我发现了比这更好的方法问题中描述的那个:
扩展 FixtureHelper 类并添加方法 haveCleanDb ,该方法调用 unloadFixtures()/ loadFixtures()。然后,在需要的Cest场景中,在 _before _after 方法中调用它: $ I-> haveCleanDb()。这样,所有其他测试仍然很快。

I found a better approach than the one described in the question: Extend the FixtureHelper class and add a method haveCleanDb which calls unloadFixtures()/loadFixtures(). Then, in the Cest scenario where I need it, I call it in the _before and _after methods: $I->haveCleanDb(). That way, all the other tests are still fast.

这篇关于Yii2:用于特定功能测试的夹具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆