<p>Is there a way to get the tests inside of a <code>TestCase</code> to run in a certain order? For example, I want to separate the life cycle of an object from creation to use to destruction but I need to make sure that the object is set up first before I run the other tests.</p>

[{'answer_id': 10321, 'author': 'Fabio Gomes', 'author_id': 727, 'author_profile': 'https://Stackoverflow.com/users/727', 'pm_score': 7, 'selected': True, 'text': '<p>Maybe there is a design problem in your tests.</p>\n\n<p>Usually each test must not depend on any other tests, so they can run in any order.</p>\n\n<p>Each test needs to instantiate and destroy everything it needs to run, that would be the perfect approach, you should never share objects and states between tests.</p>\n\n<p>Can you be more specific about why you need the same object for N tests?</p>\n'}, {'answer_id': 11224, 'author': 'jkp', 'author_id': 912, 'author_profile': 'https://Stackoverflow.com/users/912', 'pm_score': 2, 'selected': False, 'text': '<p>There really is a problem with your tests if they need to run in a certain order.  Each test should be totally independent of the others: it helps you with defect localization, and allows you to get repeatable (and therefore debuggable) results.</p>\n\n<p>Checkout <a href="http://www.xunitpatterns.com" rel="nofollow noreferrer">this site</a> for a whole load of ideas / information, about how to factor your tests in a manner where you avoid these kinds of issues.</p>\n'}, {'answer_id': 24670, 'author': 'Gary Richardson', 'author_id': 2506, 'author_profile': 'https://Stackoverflow.com/users/2506', 'pm_score': 3, 'selected': False, 'text': '<p>If you want your tests to share various helper objects and settings, you can use <code>setUp()</code>, <code>tearDown()</code> to add to the <code>sharedFixture</code> property.</p>\n'}, {'answer_id': 1915096, 'author': 'mjs', 'author_id': 11543, 'author_profile': 'https://Stackoverflow.com/users/11543', 'pm_score': 7, 'selected': False, 'text': '<p>PHPUnit supports test dependencies via the <a href="https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies" rel="noreferrer">@depends</a> annotation.</p>\n\n<p>Here is an example from the documentation where tests will be run in an order that satisfies dependencies, with each dependent test passing an argument to the next:</p>\n\n<pre><code>class StackTest extends PHPUnit_Framework_TestCase\n{\n    public function testEmpty()\n    {\n        $stack = array();\n        $this-&gt;assertEmpty($stack);\n\n        return $stack;\n    }\n\n    /**\n     * @depends testEmpty\n     */\n    public function testPush(array $stack)\n    {\n        array_push($stack, \'foo\');\n        $this-&gt;assertEquals(\'foo\', $stack[count($stack)-1]);\n        $this-&gt;assertNotEmpty($stack);\n\n        return $stack;\n    }\n\n    /**\n     * @depends testPush\n     */\n    public function testPop(array $stack)\n    {\n        $this-&gt;assertEquals(\'foo\', array_pop($stack));\n        $this-&gt;assertEmpty($stack);\n    }\n}\n</code></pre>\n\n<p>However, it\'s important to note that tests with unresolved dependencies will <em>not</em> be executed (desirable, as this brings attention quickly to the failing test). So, it\'s important to pay close attention when using dependencies.</p>\n'}, {'answer_id': 1973914, 'author': 'saleem badreddine', 'author_id': 240105, 'author_profile': 'https://Stackoverflow.com/users/240105', 'pm_score': 3, 'selected': False, 'text': "<p>PHPUnit allows the use of '@depends' annotation which specifies dependent test cases and allows passing arguments between dependent test cases.</p>\n"}, {'answer_id': 2532582, 'author': 'Bibek Shrestha', 'author_id': 84143, 'author_profile': 'https://Stackoverflow.com/users/84143', 'pm_score': 2, 'selected': False, 'text': "<p>In my view, take the following scenario where I need to test creation and destroying of a particular resource.</p>\n\n<p>Initially I had two methods, a. testCreateResource and b. testDestroyResource</p>\n\n<p>a. testCreateResource</p>\n\n<pre><code>&lt;?php\n$app-&gt;createResource('resource');\n$this-&gt;assertTrue($app-&gt;hasResource('resource'));\n?&gt;\n</code></pre>\n\n<p>b. testDestroyResource</p>\n\n<pre><code>&lt;?php\n$app-&gt;destroyResource('resource');\n$this-&gt;assertFalse($app-&gt;hasResource('resource'));\n?&gt;\n</code></pre>\n\n<p>I think this is a bad idea, as testDestroyResource depends upon testCreateResource. And a better practice would be to do</p>\n\n<p>a. testCreateResource</p>\n\n<pre><code>&lt;?php\n$app-&gt;createResource('resource');\n$this-&gt;assertTrue($app-&gt;hasResource('resource'));\n$app-&gt;deleteResource('resource');\n?&gt;\n</code></pre>\n\n<p>b. testDestroyResource</p>\n\n<pre><code>&lt;?php\n$app-&gt;createResource('resource');\n$app-&gt;destroyResource('resource');\n$this-&gt;assertFalse($app-&gt;hasResource('resource'));\n?&gt;\n</code></pre>\n"}, {'answer_id': 37879309, 'author': 'Kamil Kiełczewski', 'author_id': 860099, 'author_profile': 'https://Stackoverflow.com/users/860099', 'pm_score': 2, 'selected': False, 'text': '<p>Alternative solution:\nUse static(!) functions in your tests to create reusable elements. For instance (I use selenium IDE to record tests and phpunit-selenium (github) to run test inside browser) </p>\n\n<pre><code>class LoginTest extends SeleniumClearTestCase\n{\n    public function testAdminLogin()\n    {\n        self::adminLogin($this);\n    }\n\n    public function testLogout()\n    {\n        self::adminLogin($this);\n        self::logout($this);\n    }\n\n    public static function adminLogin($t)\n    {\n        self::login($t, \'john.smith@gmail.com\', \'pAs$w0rd\');\n        $t-&gt;assertEquals(\'John Smith\', $t-&gt;getText(\'css=span.hidden-xs\'));\n    }\n\n    // @source LoginTest.se\n    public static function login($t, $login, $pass)\n    {\n        $t-&gt;open(\'/\');\n        $t-&gt;click("xpath=(//a[contains(text(),\'Log In\')])[2]");\n        $t-&gt;waitForPageToLoad(\'30000\');\n        $t-&gt;type(\'name=email\', $login);\n        $t-&gt;type(\'name=password\', $pass);\n        $t-&gt;click("//button[@type=\'submit\']");\n        $t-&gt;waitForPageToLoad(\'30000\');\n    }\n\n    // @source LogoutTest.se\n    public static function logout($t)\n    {\n        $t-&gt;click(\'css=span.hidden-xs\');\n        $t-&gt;click(\'link=Logout\');\n        $t-&gt;waitForPageToLoad(\'30000\');\n        $t-&gt;assertEquals(\'PANEL\', $t-&gt;getText("xpath=(//a[contains(text(),\'Panel\')])[2]"));\n    }\n}\n</code></pre>\n\n<p>Ok, and now, i can use this reusable elements in other test :) For instance:</p>\n\n<pre><code>class ChangeBlogTitleTest extends SeleniumClearTestCase\n{\n    public function testAddBlogTitle()\n    {\n      self::addBlogTitle($this,\'I like my boobies\');\n      self::cleanAddBlogTitle();\n    }\n\n    public static function addBlogTitle($t,$title) {\n      LoginTest::adminLogin($t);\n\n      $t-&gt;click(\'link=ChangeTitle\');\n      ...\n      $t-&gt;type(\'name=blog-title\', $title);\n      LoginTest::logout($t);\n      LoginTest::login($t, \'paris@gmail.com\',\'hilton\');\n      $t-&gt;screenshot(); // take some photos :)\n      $t-&gt;assertEquals($title, $t-&gt;getText(\'...\'));\n    }\n\n    public static function cleanAddBlogTitle() {\n        $lastTitle = BlogTitlesHistory::orderBy(\'id\')-&gt;first();\n        $lastTitle-&gt;delete();\n    }\n</code></pre>\n\n<ul>\n<li>In this way, you can build hierarchy of you tests.</li>\n<li>You can steel keep property that each test case is totaly separate from other (if you clean DB after each test).</li>\n<li>And most important, if for instance, the way of login change in future, you only modify LoginTest class, and you don\'n need correct login part in other tests (they should work after update LoginTest) :)</li>\n</ul>\n\n<p>When I run test my script clean up db ad the begining. Above I use my <code>SeleniumClearTestCase</code> class (I make screenshot() and other nice functions there) it is  extension  of <code>MigrationToSelenium2</code> (from github, to port recorded tests in firefox using seleniumIDE + ff plugin "Selenium IDE: PHP Formatters" ) which is extension of my class LaravelTestCase (it is copy of Illuminate\\Foundation\\Testing\\TestCase but not extends PHPUnit_Framework_TestCase) which setup laravel to have access to eloquent when we want to clean DB at the end of test) which is extension of PHPUnit_Extensions_Selenium2TestCase. To set up laravel eloquent I have also in SeleniumClearTestCase function createApplication (which is called at <code>setUp</code>, and I take this function from laral test/TestCase)</p>\n'}, {'answer_id': 45323854, 'author': 'Gino Pane', 'author_id': 2018304, 'author_profile': 'https://Stackoverflow.com/users/2018304', 'pm_score': 5, 'selected': False, 'text': '<p>The correct answer for this is a proper configuration file for tests. I had the same problem and fixed it by creating testsuite with necessary test files order:</p>\n\n<pre><code>phpunit.xml:\n\n&lt;phpunit\n        colors="true"\n        bootstrap="./tests/bootstrap.php"\n        convertErrorsToExceptions="true"\n        convertNoticesToExceptions="true"\n        convertWarningsToExceptions="true"\n        strict="true"\n        stopOnError="false"\n        stopOnFailure="false"\n        stopOnIncomplete="false"\n        stopOnSkipped="false"\n        stopOnRisky="false"\n&gt;\n    &lt;testsuites&gt;\n        &lt;testsuite name="Your tests"&gt;\n            &lt;file&gt;file1&lt;/file&gt; //this will be run before file2\n            &lt;file&gt;file2&lt;/file&gt; //this depends on file1\n        &lt;/testsuite&gt;\n    &lt;/testsuites&gt;\n&lt;/phpunit&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/10228', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/204/']