Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Before and after methods for testcases #150

Closed
thekid opened this issue Oct 17, 2011 · 0 comments
Closed

Before and after methods for testcases #150

thekid opened this issue Oct 17, 2011 · 0 comments

Comments

@thekid
Copy link
Member

thekid commented Oct 17, 2011

Scope of Change

There will be a way to run methods prior to and after all tests in a
TestCase class. In contrast to setUp() and tearDown() they will be run
only once per class and not once per test.

Rationale

Initialize and dispose of resources that are expensive to create, e.g.
a server instance, a network connection, ...

Functionality

Note

This can violate the principle that unittest should be independent. For
some cases though this might be a necessary optimization to meet the 
goal that unittests should run fast - for other situations, setUp() and
tearDown() serve this purpose.

Annotations

The functionality can be used by annotating public static methods with
the @beforeClass and @afterclass annotations.

Example

<?php
  class FtpIntegrationTest extends TestCase {
    protected static $server= NULL;

    #[@beforeClass]
    public static function startServer() {
      self::$server= ...
      self::$server->start();
    }

    #[@test]
    public function connect() {
      $this->assertTrue(create(new FtpConnection('ftp://...'))->connect());
    }

    #[@afterClass]
    public static function shutdownServer() {
      self::$server->shutdown();
    }
  }
?>

Timeline

When a test suite is run, the following is performed for each test class
added to the suite:

  * Call @beforeClass method if present
  * Then, for each method annotated with @test:
    * Call setUp() method
    * Call test method
    * Call tearDown() method
  * Call @afterClass method if present.

Definitions

  • The @beforeClass and @afterclass methods must be public static.
  • There may be one @beforeClass and one @afterclass method, not more.
  • When the @beforeClass method throws an exception, no further tests
    in that class are run (and instead marked as skipped).

Security considerations

n/a

Speed impact

Slightly slower because of additional checks.

Dependencies

none.

Related documents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant