Yelp/Testify — pull request #55 by EvanKrall (2012-03-20 18:48:58+00:00)
Title: Async HTTP reporting and context manager-style fixtures

The http_reporter now reports in a separate thread. The goal is to reduce the amount of blocking network latency between running two tests - this hopefully should improve test clients' throughput.

Two new fixture types exist: `@class_setup_teardown` and `@setup_teardown`. These are single-function fixtures that perform the role of both setup and teardown.

A fixture marked with these decorators should `yield` once (the yielded value is ignored.) Any code before the `yield` will be run before each test; any code after the `yield` will run after the test. Internally, this code uses [`contextlib.contextmanager`](http://docs.python.org/library/contextlib.html#contextlib.contextmanager) to convert the function to a context manager, then uses a `with` statement to wrap test execution with the fixture.

As an example, these three test cases are roughly equivalent:

```python
from contextlib import contextmanager
from testify import assert_equal, setup_teardown, setup, teardown, TestCase

class MyTestCase1(TestCase):
@setup_teardown
def open_file(self):
self.file = open('filename.txt')
yield
self.file.close()

def test_file_is_all_whitespace(self):
for line in self.file:
assert_equal(line.strip(), '')

class MyTestCase2(TestCase):
@setup
def open_file(self):
self.file = open('filename.txt')

@teardown
def close_file(self):
self.file.close()

def test_file_is_all_whitespace(self):
for line in self.file:
assert_equal(line.strip(), '')

class MyTestCase3(TestCase):
@contextmanager
def open_file(self):
self.file = open('filename.txt')
yield
self.file.close()

def test_file_is_all_whitespace(self):
with self.open_file():
for line in self.file:
assert_equal(line.strip(), '')
```

`@setup_teardown` fixtures are entered after all `@setup` fixtures are run, and are exited before any `@teardown` fixtures are run. Similarly, `@class_setup_teardown` fixtures are entered after `@class_setup` and exited before `@class_teardown`.

I want to write more tests for http_reporter, but I also wanted to get feedback on the new fixture type, so expect there to be a few more commits added to this pull request.