Skip to content

Commit 406ba3f

Browse files
committed
Add class-based-testing fixtures and decorators
This should ease testing on legacy class-based test suites that don't want to switch to function-style test
1 parent a17b34c commit 406ba3f

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ def test_that_my_library_works_kinda_ok(httpbin_both):
6666

6767
Through the magic of pytest parametrization, this function will actually execute twice: once with an http url and once with an https url.
6868

69+
## Using pytest-httpbin with unittest-style test cases
70+
71+
I have provided 2 additional fixtures to make testing with class-based tests easier. I have also provided a couple decorators that provide some syntactic sugar around the pytest method of adding the fixtures to class-based tests. Just add the `use_class_based_httpbin` and/or `use_class_based_httpbin_secure` class decorators to your class, and then you can access httpbin using self.httpbin and self.httpbin_secure.
72+
73+
```python
74+
import pytest_httpbin
75+
76+
@pytest_httpbin.use_class_based_httpbin
77+
@pytest_httpbin.use_class_based_httpbin_secure
78+
class TestClassBassedTests(unittest.TestCase):
79+
def test_http(self):
80+
assert requests.get(self.httpbin.url + '/get').response
81+
82+
def test_http_secure(self):
83+
assert requests.get(self.httpbin_secure.url + '/get').response
84+
```
85+
6986
## Installation
7087

7188
All you need to do is this:

pytest_httpbin/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
__version__ = '0.0.2'
1+
import pytest
2+
3+
__version__ = '0.0.3'
4+
5+
use_class_based_httpbin = pytest.mark.usefixtures("class_based_httpbin")
6+
use_class_based_httpbin_secure = pytest.mark.usefixtures("class_based_httpbin_secure")

pytest_httpbin/plugin.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from httpbin import app as httpbin_app
44
from . import serve
55

6-
76
@pytest.fixture(scope='session')
87
def httpbin(request):
98
server = serve.Server(application=httpbin_app)
@@ -26,3 +25,13 @@ def httpbin_both(request, httpbin, httpbin_secure):
2625
return httpbin
2726
elif request.param == 'https':
2827
return httpbin_secure
28+
29+
30+
@pytest.fixture(scope='class')
31+
def class_based_httpbin(request, httpbin):
32+
request.cls.httpbin = httpbin
33+
34+
@pytest.fixture(scope='class')
35+
def class_based_httpbin_secure(request, httpbin_secure):
36+
request.cls.httpbin_secure = httpbin_secure
37+

tests/test_httpbin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import unittest
12
import requests
3+
import pytest_httpbin
24

35

46
def test_httpbin_gets_injected(httpbin):
@@ -30,3 +32,12 @@ def test_httpbin_join(httpbin):
3032

3133
def test_httpbin_str(httpbin):
3234
assert httpbin + '/foo' == httpbin.url + '/foo'
35+
36+
@pytest_httpbin.use_class_based_httpbin
37+
@pytest_httpbin.use_class_based_httpbin_secure
38+
class TestClassBassedTests(unittest.TestCase):
39+
def test_http(self):
40+
assert requests.get(self.httpbin.url + '/get').status_code == 200
41+
42+
def test_http_secure(self):
43+
assert requests.get(self.httpbin_secure.url + '/get').status_code == 200

0 commit comments

Comments
 (0)