PHPUnit 和 Selenium - 运行另一个类的测试


PHPUnit and Selenium - run tests from another class

我正在使用PHPUnit和Selenium来测试我的Web应用程序。

目前我有 2 个测试类 - 用户测试和权限测试

  • UserTest中,我有测试程序是否可以成功创建新用户的方法。
  • 在权限测试中,我打开和关闭某些权限并测试结果。
例如,我可能会关闭"创建用户">

权限,然后测试"创建用户"按钮是否被禁用。但是,如果我重新打开"创建用户"权限,我想测试是否可以创建用户。

能够创建用户的所有逻辑都已经在 UserTest 类中 - 那么有没有办法从 PermissionsTest 类的 UserTest 类运行测试?

目前我正在尝试以下代码:

public function testUserPermission(){
  $userTest = new UserTest();
  if($this->hasPermission = true){
    $userTest->testCanCreateUser();
  }
}

但是,当我运行此测试时,我收到错误"There is currently no active session to execute the 'execute' command. You're probably trying to set some option in setup() with an incorrect setter name..."

谢谢!

在我看来,您似乎缺少测试实现与其逻辑的分离 - 我不是在谈论PHP问题,而是在谈论通用测试模型。它将允许您在各种测试用例中重用测试组件。

你可以看看一些有关PHP中的页面对象的材料在这里或一般的硒维基。

解决方案如下:

//instantiate and set up other test class
$userTest = new UserTest();
$userTest->setUpSessionStrategy($this::$browsers[0]);
$userTest->prepareSession();
//carry out a test from this class
$userTest->testCanCreateUser();

这很好用。我不明白为什么在这种情况下使用另一个测试类的功能是一个坏主意,因为如果我不这样做,我将不得不将该功能重写到我的新类中,这似乎不那么"纯粹"......

对于硒 1 (RC(,

我进行了以下修改(以及应用页面对象设计模式(:

特定测试类

//instantiate and set up other test class
$userTest = new UserTest($this->getSessionId());
//carry out a test from this class
$userTest->createUser();
//asserts as normal
$userTest->assertTextPresent();
...

基页对象类

class PageObject extends PHPUnit_Extensions_SeleniumTestCase {
    public function __construct($session_id) {
        parent::__construct();
        $this->setSessionId($session_id);
        $this->setBrowserUrl(BASE_URL);
    }
}

特定页面对象类

class UserTest extends PageObject {
    public function createUser() {
        // Page action
    }
}