首页 > 解决方案 > 是否可以在代码接收示例中使用注释中的常量

问题描述

对于 codeception,有没有办法使用 @example 注释来使用常量(或变量)?我知道我们可以使用学说风格的注释。

数据通过 @example 注释定义,使用 JSON 或 Doctrine 样式的表示法(限于单行)。教义式

class PageCest
{
 /**
  * @example(url="/", title="Welcome")
  * @example(url="/info", title="Info")
  * @example(url="/about", title="About Us")
  * @example(url="/contact", title="Contact Us")
  */
  public function staticPages(AcceptanceTester $I, \Codeception\Example $example)
  {
    $I->amOnPage($example['url']);
    $I->see($example['title'], 'h1');
    $I->seeInTitle($example['title']);
  }
}

但是使用一个在教义中起作用的常数似乎在这里不起作用

/**
 * @example(url=Class::Constant, title="Welcome")
 */

有没有办法实现这一点来运行多个测试示例但使用常量或变量来提供值?

标签: phptestingannotationscodeception

解决方案


似乎没有办法像学说的实施那样直接做到这一点。我发现的解决方法是将常量名称作为字符串传递。

/**
 * @example(url="MyConstantName", title="Welcome")
 */

然后在代码中使用常量函数来找到你的常量值。

constant('Full\Namespace\MyClass::' . $example['MyConstantName'])

推荐阅读