Skip to content

Commit 822177b

Browse files
committed
Remove unittest TestCases, switch to pytest style for assertions
1 parent d2dce5d commit 822177b

11 files changed

+1416
-1454
lines changed

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pytest-cov
1010
pytest-django
1111
pytest-sugar
1212
django-nose
13+
ipdb

tests/conftest.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
3+
from django.core import cache as django_cache
4+
from django.contrib.auth.models import User
5+
6+
@pytest.fixture(autouse=True)
7+
def cache():
8+
django_cache.cache.clear()
9+
yield django_cache.cache
10+
11+
12+
@pytest.fixture
13+
def assert_redirect():
14+
def inner(response, expected):
15+
assert response.status_code == 302
16+
assert response['Location'] == expected
17+
18+
return inner
19+
20+
21+
22+
23+
@pytest.fixture
24+
def fake_user(db):
25+
return User.objects.create_user(username="test", password="test", email="[email protected]")
26+
27+
@pytest.fixture
28+
def fake_admin(db):
29+
return User.objects.create_user(
30+
username="admin", email="[email protected]", password="test", is_superuser=True, is_staff=True
31+
)
32+
33+
34+
@pytest.fixture
35+
def admin_client(client, fake_admin):
36+
assert client.login(username="admin", password="test") is True
37+
return client
38+
39+
40+
@pytest.fixture
41+
def user_client(client, fake_user):
42+
assert client.login(username="test", password="test") is True
43+
return client
44+
45+
46+
@pytest.fixture
47+
def henri(db):
48+
return User.objects.create_user(
49+
username="henri", password="test", email="[email protected]"
50+
)
51+
52+
53+
@pytest.fixture
54+
def henri_client(client, henri):
55+
assert client.login(username="henri", password="test") is True
56+
return client

tests/test_checkpreferences_command.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,39 @@
55
from django.test import TestCase
66

77

8-
class BaseTest(object):
9-
10-
def tearDown(self):
11-
caches["default"].clear()
12-
13-
14-
class TestCheckPreferencesCommand(BaseTest, TestCase):
15-
def call_command(self, *args, **kwargs):
16-
out = StringIO()
17-
call_command(
18-
"checkpreferences",
19-
*args,
20-
stdout=out,
21-
stderr=StringIO(),
22-
**kwargs,
23-
)
24-
return out.getvalue().strip()
25-
26-
def test_dry_run(self):
27-
out = self.call_command(verbosity=0)
28-
expected_output = "\n".join([
8+
def call(*args, **kwargs):
9+
out = StringIO()
10+
call_command(
11+
"checkpreferences",
12+
*args,
13+
stdout=out,
14+
stderr=StringIO(),
15+
**kwargs,
16+
)
17+
return out.getvalue().strip()
18+
19+
20+
def test_dry_run(db):
21+
out = call(verbosity=0)
22+
expected_output = "\n".join(
23+
[
2924
"Creating missing global preferences...",
3025
"Deleted 0 global preferences",
3126
"Deleted 0 GlobalPreferenceModel preferences",
3227
"Deleted 0 UserPreferenceModel preferences",
3328
"Creating missing preferences for User model...",
34-
])
35-
self.assertEqual(out, expected_output)
29+
]
30+
)
31+
assert out == expected_output
32+
3633

37-
def test_skip_create(self):
38-
out = self.call_command("--skip_create", verbosity=0)
39-
expected_output = "\n".join([
34+
def test_skip_create(db):
35+
out = call("--skip_create", verbosity=0)
36+
expected_output = "\n".join(
37+
[
4038
"Deleted 0 global preferences",
4139
"Deleted 0 GlobalPreferenceModel preferences",
4240
"Deleted 0 UserPreferenceModel preferences",
43-
])
44-
self.assertEqual(out, expected_output)
41+
]
42+
)
43+
assert out == expected_output

0 commit comments

Comments
 (0)