$stack
variable.setUp()
method will be called.tearDown()
.StackTest
test StackTest
such a way as to reuse not the test environment, but the code that creates it.$stack
class variable, which we will use instead of a local method variable.setUp()
method.$this->stack
class instead of the local variable of the $stack
method.<?php<br>
class StackTest extends PHPUnit_Framework_TestCase<br>
{<br>
protected $stack;<br>
<br>
protected function setUp()<br>
{<br>
$ this ->stack = array();<br>
}<br>
<br>
public function testEmpty()<br>
{<br>
$ this ->assertTrue(empty($ this ->stack));<br>
}<br>
<br>
public function testPush()<br>
{<br>
array_push($ this ->stack, 'foo' );<br>
$ this ->assertEquals( 'foo' , $ this ->stack[count($ this ->stack)-1]);<br>
$ this ->assertFalse(empty($ this ->stack));<br>
}<br>
<br>
public function testPop()<br>
{<br>
array_push($ this ->stack, 'foo' );<br>
$ this ->assertEquals( 'foo' , array_pop($ this ->stack));<br>
$ this ->assertTrue(empty($ this ->stack));<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
setUp()
and tearDown()
are called once for each test method (and for a new instance) of the test class.setUpBeforeClass()
and tearDownAfterClass()
are called before the first method of the test class is executed and after the last method is completed.<?php<br>
require_once 'PHPUnit/Framework.php' ;<br>
<br>
class TemplateMethodsTest extends PHPUnit_Framework_TestCase<br>
{<br>
public static function setUpBeforeClass()<br>
{<br>
print __METHOD__ . "\n" ;<br>
}<br>
<br>
protected function setUp()<br>
{<br>
print __METHOD__ . "\n" ;<br>
}<br>
<br>
protected function assertPreConditions()<br>
{<br>
print __METHOD__ . "\n" ;<br>
}<br>
<br>
public function testOne()<br>
{<br>
print __METHOD__ . "\n" ;<br>
$ this ->assertTrue(TRUE);<br>
}<br>
<br>
public function testTwo()<br>
{<br>
print __METHOD__ . "\n" ;<br>
$ this ->assertTrue(FALSE);<br>
}<br>
<br>
protected function assertPostConditions()<br>
{<br>
print __METHOD__ . "\n" ;<br>
}<br>
<br>
protected function tearDown()<br>
{<br>
print __METHOD__ . "\n" ;<br>
}<br>
<br>
public static function tearDownAfterClass()<br>
{<br>
print __METHOD__ . "\n" ;<br>
}<br>
<br>
protected function onNotSuccessfulTest(Exception $e)<br>
{<br>
print __METHOD__ . "\n" ;<br>
throw $e;<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
phpunit TemplateMethodsTest PHPUnit 3.4.2 by Sebastian Bergmann. TemplateMethodsTest :: setUpBeforeClass TemplateMethodsTest :: setUp TemplateMethodsTest :: assertPreConditions TemplateMethodsTest :: testOne TemplateMethodsTest :: assertPostConditions TemplateMethodsTest :: tearDown .TemplateMethodsTest :: setUp TemplateMethodsTest :: assertPreConditions TemplateMethodsTest :: testTwo TemplateMethodsTest :: tearDown TemplateMethodsTest :: onNotSuccessfulTest FTemplateMethodsTest :: tearDownAfterClass Time: 0 seconds There was 1 failure: 1) TemplateMethodsTest :: testTwo Failed asserting that <boolean: false> is true. /home/sb/TemplateMethodsTest.php:30 FAILURES! Tests: 2, Assertions: 2, Failures: 1.
setUp()
and tearDown()
should theoretically be completely symmetrical, but in practice this is not the case.tearDown()
if you have opened some external resource in setUp()
, for example, a socket or a file. If setUp()
creates only PHP objects, tearDown()
can be ignored. However, if multiple objects are created in setUp()
, it is wise to place unset()
calls for the created objects in tearDown()
.tearDown()
will perform the function of a garbage collector.setUp()
functions differ slightly, transfer the specific code from setUp()
to the test methods.setUp()
functions setUp()
very different, then you need to create another test class. Name this new class by analogy with the differences in the test environment.setUp()
and tearDown()
template methods of the setUp()
class (see section Using the TestSuite class ) to connect to the database before the first test of the test set and disconnect from the database after the last test. The $sharedFixture
attribute of the $sharedFixture
object is available in all class objects inherited from PHPUnit_Framework_TestSuite
and PHPUnit_Framework_TestCase
.<?php<br>
require_once 'PHPUnit/Framework.php' ;<br>
<br>
class DatabaseTestSuite extends PHPUnit_Framework_TestSuite<br>
{<br>
protected function setUp()<br>
{<br>
$ this ->sharedFixture = new PDO(<br>
'mysql:host=wopr;dbname=test' ,<br>
'root' ,<br>
'' <br>
);<br>
}<br>
<br>
protected function tearDown()<br>
{<br>
$ this ->sharedFixture = NULL;<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
$foo = 'bar';
saved as $GLOBALS['foo'] = 'bar';
.$GLOBALS
variable is otherwise called superglobal .$foo
or using direct access$GLOBALS['foo']
, or using the global $foo;
keyword global $foo;
to create a local variable that will be associated with a global one.$GLOBALS
, $_ENV
, $_POST
, $_GET
, $_COOKIE
, $_SERVER
, $_FILES
, $_REQUEST
) does not affect other tests. Additionally, this isolation can be extended to static class attributes.serialize()
and unserialize()
functions.PDO
, cannot be saved and restored.$GLOBALS
array will fail with an error.@backupGlobals
annotation, see the section @backupGlobals
.class MyTest extends PHPUnit_Framework_TestCase { protected $ backupGlobalsBlacklist = array ('globalVariable'); // ... }
$backupGlobalsBlacklist
attribute inside a method, for example, setUp()
will not have an effect.@backupStaticAttributes
annotation, see the @backupStaticAttributes
section @backupStaticAttributes
class MyTest extends PHPUnit_Framework_TestCase { protected $ backupStaticAttributesBlacklist = array ( 'className' => array ('attributeName') ); // ... }
$backupStaticAttributesBlacklist
attribute inside a method, for example, setUp()
will have no effect.Source: https://habr.com/ru/post/89581/
All Articles