Class phpunit:如何在测试之间传递值?
我真的遇到了麻烦。如何在phpunit中的测试之间传递类值 测试1->设置值 测试2->读取值 这是我的密码:Class phpunit:如何在测试之间传递值?,class,phpunit,Class,Phpunit,我真的遇到了麻烦。如何在phpunit中的测试之间传递类值 测试1->设置值 测试2->读取值 这是我的密码: class JsonRpcBitcoinTest extends PHPUnit_Framework_TestCase { public function setUp(){ global $configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort; $this->bitco
class JsonRpcBitcoinTest extends PHPUnit_Framework_TestCase
{
public function setUp(){
global $configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort;
$this->bitcoindConn = new JsonRpcBitcoin($configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort);
$this->blockHash = '';
}
/**
* @depends testCanAuthenticateToBitcoindWithGoodCred
*/
public function testCmdGetBlockHash()
{
$result = (array)json_decode($this->bitcoindConn->getblockhash(20));
$this->blockHash = $result['result'];
$this->assertNotNull($result['result']);
}
/**
* @depends testCmdGetBlockHash
*/
public function testCmdGetBlock()
{
$result = (array)json_decode($this->bitcoindConn->getblock($this->blockHash));
$this->assertEquals($result['error'], $this->blockHash);
}
}
testCmdGetBlock()
未获取应在testCmdGetBlockHash()
中设置的$this->blockHash
的值
非常感谢帮助理解错误。在测试之前总是调用setUp()
方法,因此即使您在两个测试之间设置了依赖项,在setUp()
中设置的任何变量都将被覆盖。PHPUnit数据传递的工作方式是从一个测试的返回值到另一个测试的参数:
class JsonRpcBitcoinTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
global $configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort;
$this->bitcoindConn = new JsonRpcBitcoin($configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort);
$this->blockHash = '';
}
public function testCmdGetBlockHash()
{
$result = (array)json_decode($this->bitcoindConn->getblockhash(20));
$this->assertNotNull($result['result']);
return $result['result']; // the block hash
}
/**
* @depends testCmdGetBlockHash
*/
public function testCmdGetBlock($blockHash) // return value from above method
{
$result = (array)json_decode($this->bitcoindConn->getblock($blockHash));
$this->assertEquals($result['error'], $blockHash);
}
}
因此,如果您需要在测试之间保存更多的状态,请在该方法中返回更多数据。我猜PHPUnit之所以让人讨厌,是因为它不鼓励依赖测试
.您可以在函数中使用静态变量。。。
PHP讨厌地与所有实例共享类方法的静态变量。。。但在这种情况下,它会有所帮助:p
protected function &getSharedVar()
{
static $value = null;
return $value;
}
...
public function testTest1()
{
$value = &$this->getSharedVar();
$value = 'Hello Test 2';
}
public function testTest2()
{
$value = &$this->getSharedVar();
// $value should be ok
}
注意:这不是一个好方法,但如果您在所有测试中都需要一些数据,它会有所帮助。这在所有测试中都非常适合我:$this->varablename
class SignupTest extends TestCase
{
private $testemail = "registerunittest@company.com";
private $testpassword = "Mypassword";
public $testcustomerid = 123;
private $testcountrycode = "+1";
private $testphone = "5005550000";
public function setUp(): void
{
parent::setUp();
}
public function tearDown(): void
{
parent::tearDown();
}
public function testSignup()
{
$this->assertEquals("5005550000", $this->testphone;
}
}
另一种选择是使用静态变量 以下是一个示例(针对Symfony 4功能测试):
好极了非常感谢你,我不知道我怎么会不知道。谢谢你分享这个答案。希望有一种方法可以声明一个可在测试之间共享的可变变量,但这似乎足够优雅。您可能会强调您指的是“@depends”注释,我花了一段时间才弄明白。。。不管怎样,回答得很好。当你需要获取或设置类中所有测试的配置时,这个答案非常有用。在类中的所有测试之前,没有像setUpPage这样的会话上下文只调用一次的方法。Em我说的对吗?通常我已经找到了公共静态函数setUpBeforeClass(),但这个函数是在测试类实例化之前调用的。“PHP讨厌地与所有实例共享类方法的静态变量”-这就是静态变量的全部要点!它们的存在正是为了这个目的,在类的所有实例之间共享值。我不明白为什么这会很烦人,如果你不想这样,那么就不要使用静态变量。请阅读关于PHP中静态变量的文档。非静态方法中的静态变量可能以实例范围为中心,而不是以类范围为中心。我觉得OOP更友好,更有用。谢谢你这么说。当你不知道这一点时,会非常沮丧。这并不能回答OP的场景,即必须有2个测试,1个设置值,1个获取值。你只有一个测试可以测试。
namespace App\Tests\Controller\Api;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
use Symfony\Component\HttpFoundation\AcceptHeader;
class BasicApiTest extends WebTestCase
{
// This trait provided by HautelookAliceBundle will take care of refreshing the database content to a known state before each test
use RefreshDatabaseTrait;
private $client = null;
/**
* @var string
*/
private const APP_TOKEN = 'token-for-tests';
/**
* @var string
*/
private static $app_user__email = 'tester+api+01@localhost';
/**
* @var string
*/
private static $app_user__pass = 'tester+app+01+password';
/**
* @var null|string
*/
private static $app_user__access_token = null;
public function test__Authentication__Login()
{
$this->client->request(
Request::METHOD_POST,
'/api/login',
[],
[],
[
'CONTENT_TYPE' => 'application/json',
'HTTP_App-Token' => self::APP_TOKEN
],
'{"user":"'.static::$app_user__email.'","pass":"'.static::$app_user__pass.'"}'
);
$response = $this->client->getResponse();
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$content_type = AcceptHeader::fromString($response->headers->get('Content-Type'));
$this->assertTrue($content_type->has('application/json'));
$responseData = json_decode($response->getContent(), true);
$this->assertArrayHasKey('token', $responseData);
$this->static = static::$app_user__access_token = $responseData['token'];
}
/**
* @depends test__Authentication__Login
*/
public function test__SomeOtherTest()
{
$this->client->request(
Request::METHOD_GET,
'/api/some_endpoint',
[],
[],
[
'CONTENT_TYPE' => 'application/json',
'HTTP_App-Token' => self::APP_TOKEN,
'HTTP_Authorization' => 'Bearer ' . static::$app_user__access_token
],
'{"user":"'.static::$app_user__email.'","pass":"'.static::$app_user__pass.'"}'
);
$response = $this->client->getResponse();
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$content_type = AcceptHeader::fromString($response->headers->get('Content-Type'));
$this->assertTrue($content_type->has('application/json'));
//...
}
}