简体   繁体   中英

How to write right Cest format tests in codeception?

I noticed, that whenever Cest type testscases are run, they preserve their sessions after completion. In other words, it is possible for other tests to continue from the place, where the test before left off . Example:

class CheckGoogleSearch{
  public function checkIfPageIsAccessible(AcceptanceTester $I){
    $I->amOnPage('/');
    $I->see('something..');
  }

  public function checkIfSearchFieldIsAccessible(AcceptanceTester $I){
    //  Notice, that it is assumed that we are on the google's home page,
    //because the above test had it already opened in the past
    $I->see('Google Search');
  }
}

Can this code be considered a best practice? Or will it be error-prone in the future and would it be better to reset to Google's home page(and make some additional preparations) before each test?

You can use the

@depends

annotation. This way if your first test failed, the next test that depends on the previous one, will be skipped and not reported a fail.

class CheckGoogleSearch{
  public function checkIfPageIsAccessible(AcceptanceTester $I){
    $I->amOnPage('/');
    $I->see('something..');
  }

 /**
  * @param AcceptanceTester $I
  * @depends login
  */
  public function checkIfSearchFieldIsAccessible(AcceptanceTester $I){
    //  Notice, that it is assumed that we are on the google's home page,
    //because the above test had it already opened in the past
    $I->see('Google Search');
  }
}

Personnaly I don't think this either good or bad practice. It depends on your testing stack. In your example if you only have codeception, and no other integrations that need finer details, doing all as on test id fine, but my case I have a Testlink integration with a test case for

  1. Can I open / access the page
  2. Are certain elements available
  3. Can I do a search and get the expected results

Hope this helps.

Happy testing!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM