【问题标题】:Codeception test cases preconditionCodeception 测试用例前置条件
【发布时间】:2017-06-16 22:16:06
【问题描述】:

我正在使用 codeception 框架编写自动化测试。我有一个测试用例在用户登录后验证某些功能。大约有 20 个具有不同功能的测试用例。所有的测试用例都需要用户登录系统,所以我在_before回调下写了一个登录功能。当我执行所有测试用例时,在检查每个测试用例登录功能之前会花费大量时间。我们是否可以编写登录功能作为前提条件,一旦用户登录,它应该执行所有测试用例?

【问题讨论】:

    标签: php phpunit codeception


    【解决方案1】:

    您可以在代码接收中使用所谓的 Helpers。您可以使用以下命令生成帮助程序:

    vendor/bin/codecept generate:helper Login

    然后你可以在类中放入一个方法来登录用户,如下所示:

    <?php
    namespace Helper;
    
    // here you can define custom actions
    // all public methods declared in helper class will be available in $I
    
    class Login extends \Codeception\Module
    {
        public function login($username, $password)
        {
            /** @var \Codeception\Module\WebDriver $webDriver */
            $webDriver = $this->getModule('WebDriver');
            // if snapshot exists - skipping login
            if ($webDriver->loadSessionSnapshot('login')) {
                return;
            }
            // logging in
            $webDriver->amOnPage('/login');
            $webDriver->submitForm('#loginForm', [
                'login' => $username,
                'password' => $password
            ]);
            $webDriver->see($username, '.navbar');
            // saving snapshot
            $webDriver->saveSessionSnapshot('login');
        }
    }
    

    有关快照的更多信息,请参阅http://codeception.com/docs/06-ReusingTestCode#session-snapshot

    您的acceptance.suite.yml 应该如下所示:

    # Codeception Test Suite Configuration
    #
    # Suite for acceptance tests.
    # Perform tests in browser using the WebDriver or PhpBrowser.
    # If you need both WebDriver and PHPBrowser tests - create a separate suite.
    
    class_name: AcceptanceTester
    modules:
        enabled:
            # Note we must use WebDriver for us to use session snapshots
            - WebDriver:
                url: http://localhost/myapp
                browser: chrome
    
            - \Helper\Acceptance
    
            # Note that we must add the Login Helper class we generated here
            - \Helper\Login
    

    现在我们有了一个可以在所有测试中重用的辅助类。我们来看一个例子:

    <?php
    
    
    class UserCest
    {
        // tests
        public function testUserCanLogin(AcceptanceTester $I)
        {
            $I->login('username', 'password');
        }
    
        public function testUserCanCarryOutTask(AcceptanceTester $I)
        {
            $I->login('username', 'password');
            $I->amOnPage('/task-page');
            $I->see('Task Page');
            // other assertions below
        }
    
        public function testUserCanCarryOutAnotherTask(AcceptanceTester $I)
        {
            $I->login('username', 'password');
            $I->amOnPage('/another-task-page');
            $I->see('Another Task Page');
            // other assertions below
        }
    }
    

    现在运行 UserCest 测试时,它应该只登录用户一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-10
      • 2013-09-07
      • 2016-10-11
      • 2018-10-06
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2019-12-04
      相关资源
      最近更新 更多