18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPUnitで定数のテストをするときは@runInSeparateProcessで別プロセスに

Last updated at Posted at 2012-03-16

定数を動的に定義するようなプログラムを一つのテストプロセスでやってしまうと、定数は上書きできないためうまくテストできない。

そんなときはテストを別プロセスにすることでテスタブルになる。

やり方は @runInSeparateProcess アノテーションを別プロセスにしたいテストにつけるだけ。

<?php
class ConstantTest extends PHPUnit_Framework_TestCase
{
	/**
	 * @runInSeparateProcess
	 */
	public function testConstant1()
	{
		define('THIS_IS_CONSTANT', 'foo');
		$this->assertSame('foo', THIS_IS_CONSTANT);
	}

	/**
	 * @runInSeparateProcess
	 */
	public function testConstant2()
	{
		define('THIS_IS_CONSTANT', 'bar');
		$this->assertSame('bar', THIS_IS_CONSTANT);
	}

	/**
	 * @runInSeparateProcess
	 */
	public function testConstant3()
	{
		define('THIS_IS_CONSTANT', 'baz');
		$this->assertSame('baz', THIS_IS_CONSTANT);
	}
}

実行結果

PHPUnit 3.6.3 by Sebastian Bergmann.

...

Time: 0 seconds, Memory: 4.75Mb

OK (3 tests, 3 assertions)
18
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?