【问题标题】:Codeception run before all tests inside a CESTCodeception 在 CEST 内的所有测试之前运行
【发布时间】:2016-08-21 13:34:51
【问题描述】:

我想在特定 Cest 中的所有测试之前运行一些东西,然后在所有测试运行后清理它,类似于 PHPUnit 中的 setUpBeforeClass 和 tearDownAfterClass 方法。

有没有办法在 Codeception 中做这样的事情?

【问题讨论】:

    标签: php codeception


    【解决方案1】:

    在 Codeception 的人给你可靠的方法之前,我现在有一个粗略的解决方案。

    只需在所有现有actor(测试用例)之上创建另一个actor,如下所示:

    class MyCest
    {
    
    function _before(AcceptanceTester $I)
    {
        $I->amOnPage('/mypage.php');
    }
    
    public function _after(AcceptanceTester $I)
    {
    
    }
    
    function beforeAllTests(AcceptanceTester $I,\Page\MyPage $myPage,\Helper\myHelper $helper){
        //Do what you have to do here - runs only once before all below tests
        //Do something with above arguments
    }
    
    public function myFirstTest(AcceptanceTester $I){
        $I->see('Hello World');
    }
    
    function afterAllTests(){
        //For something after all tests
    }
    }
    

    您可以将 beforeAllTests 函数设为公共但不受保护,也不应以“_”开头,以便它在所有测试之前运行。

    另一组函数在所有测试开始之前只运行一次,应该在 /tests/_support/Helper/Acceptance.php 中实例化以进行验收等等。在这里你可以调用函数:

    // HOOK: used after configuration is loaded
    public function _initialize()
    {
    }
    
    // HOOK: before each suite
    public function _beforeSuite($settings = array())
    {
    }
    

    更多功能,请访问:https://codeception.com/docs/06-ModulesAndHelpers#Hooks

    【讨论】:

      【解决方案2】:

      从 Codeception 的角度来看,Cest 类只是一堆 Cept 场景。 没有对象范围,也没有前/后类挂钩。

      我的建议是改用测试格式并使用 PhpUnit 挂钩。

      测试格式扩展了 PHPUnit_Framework_TestCase,所以 setUpBeforeClass 应该可以工作。

      【讨论】:

        【解决方案3】:

        您可以在functional.suite.yml 中附加新的助手:

        class_name: FunctionalTester
        modules:
            enabled:
              - tests\components\helpers\MyHelper
        

        在 helper 中,您可以使用 _before 和 _after 方法:

        class FixtureHelper extends \Codeception\Module
        {
            /**
             * Method is called before test file run
             */
            public function _before(\Codeception\TestCase $test)
            {
                // TODO: Change the autogenerated stub
            }
        
            /**
             * Method is called after test file run
             */
            public function _after(TestCase $test)
            {
                // TODO: Change the autogenerated stub
            }
        }
        

        TestCase 方法可以帮助您确定执行_before 和_after 的必要性。

        【讨论】:

        • 我不确定我是否遵循。 _before 和 _after 不是在每个测试用例之前运行吗?如何使用这种方法区分 Cest?
        • @Casteurr 你需要写一些代码。 TestCase 有一个 getTestFullName。这可能会有所帮助。
        • 这似乎太复杂了。我需要这样的东西phpunit.de/manual/current/en/…
        • 链接中的方法在 Codeception 单元测试中完美运行。省略第一行并使用类似这样的内容作为下一行:class YourClassTest extends \Codeception\Test\Unit
        【解决方案4】:

        根据您所说的“运行某些东西”和“清理它”的含义,您可以使用 PHP 标准的构造函数和析构函数。

        这个解决方案对我来说似乎更清楚,但请记住,您无法访问 AcceptanceTester $I 和 Scenario $scenario 从那里开始,所以当你不需要它们时使用它。

        class YourCest
        {
            private Faker\Generator $faker;
            private string $email;   
        
            public function __construct()
            {
                // "Run something" here
                $this->faker = Faker\Factory::create();
                $this->email = $this->faker->email;
            }
            
            public function __destruct()
            {
                // "and then clean it up" there
            }
        
            public function tryToTest(AcceptanceTester $I)
            {
                // Do your tests here
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2017-04-03
          • 1970-01-01
          • 2016-12-07
          • 2015-05-13
          • 2020-03-04
          • 2023-04-11
          • 1970-01-01
          • 1970-01-01
          • 2018-10-06
          相关资源
          最近更新 更多