我想检查这样的事情:
<?php
$I->amOnPage('/go/google');
$I->seeCurrentUrlEquals('http://google.com');
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://google.com'
+''
Scenario Steps:
2. I see current url equals "http://google.com"
1. I am on page "/go/google"
Run Code Online (Sandbox Code Playgroud)
想法只是检查用户是否被重定向到外部资源。
我试图为我的Symfony2项目设置代码,该项目已经有4个工作包和他们的PHPUnit测试用例.现在我想添加代码测试测试用例,特别是对于验收测试,但是当我"引导"代码时,我在主结构中找到了所有生成的代码.由于我的应用程序已经捆绑在一起,我想保留这个概念以及我的测试用例.特别是因为我的bundle中已经有了我的PHPUnit测试用例.官方的s2代码并没有真正涵盖这一点.所以对我来说有些问题出现了:
我已经多次浏览文档,并通过源代码,但它不起作用.
我正在寻找与PHPUnit的"this-> assertTrue($ var)"相当的Codeception.
根据文档,就像那应该工作,但它没有,"未定义的方法".我必须激活哪个助手,模块或其他什么?
如何为调用同一类的其他方法但不返回值的方法编写单元测试?(让我们说PHPUnit.)
例如,假设我有以下类:
class MyClass {
public function doEverything() {
$this->doA;
$this->doB;
$this->doC;
}
public function doA() {
// do something, return nothing
}
public function doB() {
// do something, return nothing
}
public function doC() {
// do something, return nothing
}
}
Run Code Online (Sandbox Code Playgroud)
你会怎么测试doEverything()?
编辑:
我问这个是因为从我看过的内容看来,几乎所有的方法都应该有自己的专用单元测试.当然,你也有功能和集成测试,但那些目标特定的例程,可以这么说(不是必须在每个方法级别).
但是,如果几乎所有方法都需要自己的单元测试,我认为对所有上述方法进行单元测试将是"最佳实践".是/否?
我在Yii2中使用Codeception进行验收测试,并且无法访问我的模型,因为名称空间不适用于这些测试.
我在我的tests/_bootstrap.php中有这个
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../console/config/main.php');
//
$application = new yii\console\Application( $config );
## Added (@vitalik_74)
Yii::setAlias('@tests', dirname(__DIR__));
Run Code Online (Sandbox Code Playgroud)
这在我的console/config/main中
<?php
$params = array_merge(
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
'params' => $params,
];
<?php
return [
'adminEmail' …Run Code Online (Sandbox Code Playgroud) 我正在使用Codeception做WebServices测试,这是我的代码:
//Making first query for getting needed parameter
$I->wantTo('Make something');
$I->sendPOST($this->route, [
'token' => Fixtures::get('token'),
'id' => Fixtures::get('some_id')
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContains('"error_message"');
//Then I get what I need with regexp
if (preg_match('/^.*\s(?<value_i_need>\d+)\.$/', $I->grabDataFromResponseByJsonPath('$.status.error_message')[0], $matches)) {
$I->sendPOST($this->route, [
'token' => Fixtures::get('token'),
'id' => Fixtures::get('some_id')
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContains('"something"');
$I->seeResponseContains('"something_else"');
} else {
//And if I don't get needed parameter with regular expression, here I have to force test fail
}
Run Code Online (Sandbox Code Playgroud)
有人知道如何强制测试失败吗?
提前致谢!
我在testRegisterNewUser 中创建一个新用户并将用户名保存在类字段变量中:
private $_userName;
protected function userName ()
{
if (empty($this->_userName)) {
$this->_userName = uniqid('test_');
}
return $this->_userName;
}
public function testRegisterNewUser()
{
$user = new User($this->userName() ); // create user with random name
}
Run Code Online (Sandbox Code Playgroud)
之后,我尝试在另一个类方法中使用这个值,但是$_userName没有用testRegisterNewUser 的原始值初始化!
public function testFundUser()
{
$user = User::findByUsername( $this->userName() ); // fails, $this->userName() gives "non original" value
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我运行测试时codecept run,
这是否意味着Codeception\TestCase\Test为每个 test* 方法调用重新创建了类?
我正在使用 Codeception 进行我的第一次验收测试。
当我使用wait()或运行测试时waitForElement(),我收到以下消息:
[RuntimeException] Call to undefined method AcceptanceTester::wait
Run Code Online (Sandbox Code Playgroud)
这是我的 acceptance.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: WebGuy
modules:
enabled:
- WebDriver
- \Helper\Acceptance
config:
WebDriver:
url: 'http://rh.dev'
browser: 'firefox'
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
$I = new AcceptanceTester($scenario);
$I->wantTo('Register my profile for the first time');
$I->amOnPage('/register');
$I->fillField('name', $person->name); …Run Code Online (Sandbox Code Playgroud) 我有本地安装的JenkinsCI.通过Jenkins The Definitive Guide第2章中的说明进行安装.我在运行El Capitan的MAC上通过Java Web Start/JNLP文件启动Jenkins.一切顺利,示例项目正在运行.
我知道想通过Jenkins运行我的Codeception Acceptance测试.我正在关注Codeception网站上关于此的最新博文:http://codeception.com/02-04-2015/setting-up-jenkins-with-codeception.html#.VwWxE2PLRAZ .
使用Execute shell构建步骤,我的构建失败并显示以下消息:
用户匿名启动
在工作区中构建/Users/Cosette/.jenkins/workspace/Project名称
[项目名称] $/bin/sh -xe /var/folders/ns/ly6hv_513tl6qqslrb2vj_dw0000gn/T/hudson9210778078639547082.sh
- 作曲家安装
/var/folders/ns/ly6hv_513tl6qqslrb2vj_dw0000gn/T/hudson9210778078639547082.sh:line 2:composer:command not found
构建步骤'执行shell'将构建标记为失败
完成:失败
我的猜测是,这个安装类型可能没有安装Jenkins用户?请注意我是初学者级别,这是我在stackoverflow上的第一个问题.此外,这应该是"作曲家安装"前面的+号.
我正在尝试使用Codeception在测试工具中使用Symfony控制器。每种方法的开始如下:
public function saveAction(Request $request, $id)
{
// Entity management
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
/* Actual code here
...
*/
}
public function submitAction(Request $request, $id)
{
// Entity management
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
/* 200+ lines of procedural code here
...
*/
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
$request = \Symfony\Component\HttpFoundation\Request::create(
$uri, $method, $parameters, $cookies, $files, $server, $content);
$my_controller = new MyController();
$my_controller->submitAction($request, $id);
Run Code Online (Sandbox Code Playgroud)
从我的单元测试来看,但是似乎还有很多其他设置我不知道Symfony在后台执行。每当我找到一个丢失的对象并将其初始化时,就会有另一个在某个时刻失败。
我也尝试过逐步从PhpStorm进行测试,但是PhpUnit的某些输出会导致Symfony在它接近我要测试的代码之前就死掉,因为它无法在$_SESSION发生任何输出后启动。我不认为这是从命令行发生的,但是我还不够接近。
如何在单元测试中简单且可扩展地运行此代码?
一点背景:
我继承了这段代码。我知道它很脏而且有异味,因为它正在控制器中执行模型逻辑。我知道我要的不是“纯粹的”单元测试,因为它实际上涉及整个应用程序。
但是我需要能够自动运行这一小段(200行以上)的代码。该代码应在不超过几秒钟的时间内运行。我不知道要多久,因为我从未能够独立运行它。
当前,通过网站运行此代码的设置时间非常长,而且很复杂。该代码不会生成网页,基本上是生成文件的API调用。在进行代码更改时,我需要能够在短时间内生成尽可能多的这些测试文件。
代码就是它。能够对其进行更改是我的工作,现在我什至每次都没有大量开销就无法运行它。在不知道它在做什么的情况下进行更改是不负责任的。
codeception ×10
php ×5
testing ×3
unit-testing ×3
phpunit ×2
symfony ×2
composer-php ×1
jenkins ×1
laravel ×1
namespaces ×1
psr-4 ×1
yii2 ×1