1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
import pytest
from django.test import SimpleTestCase, TestCase, tag
from .helpers import DjangoPytester
from pytest_django_test.app.models import Item
class TestFixtures(TestCase):
fixtures = ("items",)
def test_fixtures(self) -> None:
assert Item.objects.count() == 1
assert Item.objects.get().name == "Fixture item"
def test_fixtures_again(self) -> None:
"""Ensure fixtures are only loaded once."""
self.test_fixtures()
class TestSetup(TestCase):
def setUp(self) -> None:
"""setUp should be called after starting a transaction"""
assert Item.objects.count() == 0
Item.objects.create(name="Some item")
Item.objects.create(name="Some item again")
def test_count(self) -> None:
self.assertEqual(Item.objects.count(), 2) # noqa: PT009
assert Item.objects.count() == 2
Item.objects.create(name="Foo")
self.assertEqual(Item.objects.count(), 3) # noqa: PT009
def test_count_again(self) -> None:
self.test_count()
def tearDown(self) -> None:
"""tearDown should be called before rolling back the database"""
assert Item.objects.count() == 3
class TestFixturesWithSetup(TestCase):
fixtures = ("items",)
def setUp(self) -> None:
assert Item.objects.count() == 1
Item.objects.create(name="Some item")
def test_count(self) -> None:
assert Item.objects.count() == 2
Item.objects.create(name="Some item again")
def test_count_again(self) -> None:
self.test_count()
def tearDown(self) -> None:
assert Item.objects.count() == 3
@tag("tag1", "tag2")
class TestDjangoTagsToPytestMarkers(SimpleTestCase):
"""Django test tags are converted to Pytest markers, at the class & method
levels."""
@pytest.fixture(autouse=True)
def gimme_my_markers(self, request: pytest.FixtureRequest) -> None:
self.markers = {m.name for m in request.node.iter_markers()}
@tag("tag3", "tag4") # type: ignore[misc]
def test_1(self) -> None:
assert self.markers == {"tag1", "tag2", "tag3", "tag4"}
def test_2(self) -> None:
assert self.markers == {"tag1", "tag2"}
@tag("tag5") # type: ignore[misc]
def test_3(self) -> None:
assert self.markers == {"tag1", "tag2", "tag5"}
@tag("tag1")
class TestNonDjangoClassWithTags:
"""Django test tags are only converted to Pytest markers if actually
Django tests. Use pytest markers directly for pytest tests."""
@pytest.fixture(autouse=True)
def gimme_my_markers(self, request: pytest.FixtureRequest) -> None:
self.markers = {m.name for m in request.node.iter_markers()}
@tag("tag2") # type: ignore[misc]
def test_1(self) -> None:
assert not self.markers
def test_sole_test(django_pytester: DjangoPytester) -> None:
"""
Make sure the database is configured when only Django TestCase classes
are collected, without the django_db marker.
Also ensures that the DB is available after a failure (#824).
"""
django_pytester.create_test_module(
"""
import os
from django.test import TestCase
from django.conf import settings
from .app.models import Item
class TestFoo(TestCase):
def test_foo(self):
# Make sure we are actually using the test database
_, db_name = os.path.split(settings.DATABASES['default']['NAME'])
assert db_name.startswith('test_') or db_name == ':memory:' \\
or 'file:memorydb' in db_name
# Make sure it is usable
assert Item.objects.count() == 0
assert 0, "trigger_error"
class TestBar(TestCase):
def test_bar(self):
assert Item.objects.count() == 0
"""
)
result = django_pytester.runpytest_subprocess("-v")
result.stdout.fnmatch_lines(
[
"*::test_foo FAILED",
"*::test_bar PASSED",
'> assert 0, "trigger_error"',
"E AssertionError: trigger_error",
"E assert 0",
"*= 1 failed, 1 passed*",
]
)
assert result.ret == 1
class TestUnittestMethods:
"Test that setup/teardown methods of unittests are being called."
def test_django(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import TestCase
class TestFoo(TestCase):
@classmethod
def setUpClass(self):
print('\\nCALLED: setUpClass')
def setUp(self):
print('\\nCALLED: setUp')
def tearDown(self):
print('\\nCALLED: tearDown')
@classmethod
def tearDownClass(self):
print('\\nCALLED: tearDownClass')
def test_pass(self):
pass
"""
)
result = django_pytester.runpytest_subprocess("-v", "-s")
result.stdout.fnmatch_lines(
[
"CALLED: setUpClass",
"CALLED: setUp",
"CALLED: tearDown",
"PASSED*",
"CALLED: tearDownClass",
]
)
assert result.ret == 0
def test_setUpClass_not_being_a_classmethod(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import TestCase
class TestFoo(TestCase):
def setUpClass(self):
pass
def test_pass(self):
pass
"""
)
result = django_pytester.runpytest_subprocess("-v", "-s")
expected_lines = [
"* ERROR at setup of TestFoo.test_pass *",
"E * TypeError: *",
]
result.stdout.fnmatch_lines(expected_lines)
assert result.ret == 1
def test_setUpClass_multiple_subclasses(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import TestCase
class TestFoo(TestCase):
@classmethod
def setUpClass(cls):
super(TestFoo, cls).setUpClass()
def test_shared(self):
pass
class TestBar(TestFoo):
def test_bar1(self):
pass
class TestBar2(TestFoo):
def test_bar21(self):
pass
"""
)
result = django_pytester.runpytest_subprocess("-v")
result.stdout.fnmatch_lines(
[
"*TestFoo::test_shared PASSED*",
"*TestBar::test_bar1 PASSED*",
"*TestBar::test_shared PASSED*",
"*TestBar2::test_bar21 PASSED*",
"*TestBar2::test_shared PASSED*",
]
)
assert result.ret == 0
def test_setUpClass_mixin(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import TestCase
class TheMixin:
@classmethod
def setUpClass(cls):
super(TheMixin, cls).setUpClass()
class TestFoo(TheMixin, TestCase):
def test_foo(self):
pass
class TestBar(TheMixin, TestCase):
def test_bar(self):
pass
"""
)
result = django_pytester.runpytest_subprocess("-v")
result.stdout.fnmatch_lines(["*TestFoo::test_foo PASSED*", "*TestBar::test_bar PASSED*"])
assert result.ret == 0
def test_setUpClass_skip(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import TestCase
import pytest
class TestFoo(TestCase):
@classmethod
def setUpClass(cls):
if cls is TestFoo:
raise pytest.skip("Skip base class")
super(TestFoo, cls).setUpClass()
def test_shared(self):
pass
class TestBar(TestFoo):
def test_bar1(self):
pass
class TestBar2(TestFoo):
def test_bar21(self):
pass
"""
)
result = django_pytester.runpytest_subprocess("-v")
result.stdout.fnmatch_lines(
[
"*TestFoo::test_shared SKIPPED*",
"*TestBar::test_bar1 PASSED*",
"*TestBar::test_shared PASSED*",
"*TestBar2::test_bar21 PASSED*",
"*TestBar2::test_shared PASSED*",
]
)
assert result.ret == 0
def test_multi_inheritance_setUpClass(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import TestCase
# Using a mixin is a regression test, see #280 for more details:
# https://github.com/pytest-dev/pytest-django/issues/280
class SomeMixin:
pass
class TestA(SomeMixin, TestCase):
expected_state = ['A']
state = []
@classmethod
def setUpClass(cls):
super(TestA, cls).setUpClass()
cls.state.append('A')
@classmethod
def tearDownClass(cls):
assert cls.state.pop() == 'A'
super(TestA, cls).tearDownClass()
def test_a(self):
assert self.state == self.expected_state
class TestB(TestA):
expected_state = ['A', 'B']
@classmethod
def setUpClass(cls):
super(TestB, cls).setUpClass()
cls.state.append('B')
@classmethod
def tearDownClass(cls):
assert cls.state.pop() == 'B'
super(TestB, cls).tearDownClass()
def test_b(self):
assert self.state == self.expected_state
class TestC(TestB):
expected_state = ['A', 'B', 'C']
@classmethod
def setUpClass(cls):
super(TestC, cls).setUpClass()
cls.state.append('C')
@classmethod
def tearDownClass(cls):
assert cls.state.pop() == 'C'
super(TestC, cls).tearDownClass()
def test_c(self):
assert self.state == self.expected_state
"""
)
result = django_pytester.runpytest_subprocess("-vvvv", "-s")
assert result.parseoutcomes()["passed"] == 6
assert result.ret == 0
def test_unittest(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from unittest import TestCase
class TestFoo(TestCase):
@classmethod
def setUpClass(self):
print('\\nCALLED: setUpClass')
def setUp(self):
print('\\nCALLED: setUp')
def tearDown(self):
print('\\nCALLED: tearDown')
@classmethod
def tearDownClass(self):
print('\\nCALLED: tearDownClass')
def test_pass(self):
pass
"""
)
result = django_pytester.runpytest_subprocess("-v", "-s")
result.stdout.fnmatch_lines(
[
"CALLED: setUpClass",
"CALLED: setUp",
"CALLED: tearDown",
"PASSED*",
"CALLED: tearDownClass",
]
)
assert result.ret == 0
def test_setUpClass_leaf_but_not_in_dunder_dict(self, django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import testcases
class CMSTestCase(testcases.TestCase):
pass
class FooBarTestCase(testcases.TestCase):
@classmethod
def setUpClass(cls):
print('FooBarTestCase.setUpClass')
super(FooBarTestCase, cls).setUpClass()
class TestContact(CMSTestCase, FooBarTestCase):
def test_noop(self):
print('test_noop')
"""
)
result = django_pytester.runpytest_subprocess("-q", "-s")
result.stdout.fnmatch_lines(["*FooBarTestCase.setUpClass*", "*test_noop*", "1 passed*"])
assert result.ret == 0
class TestCaseWithDbFixture(TestCase):
pytestmark = pytest.mark.usefixtures("db")
def test_simple(self) -> None:
# We only want to check setup/teardown does not conflict
assert 1
class TestCaseWithTrDbFixture(TestCase):
pytestmark = pytest.mark.usefixtures("transactional_db")
def test_simple(self) -> None:
# We only want to check setup/teardown does not conflict
assert 1
def test_pdb_enabled(django_pytester: DjangoPytester) -> None:
"""
Make sure the database is flushed and tests are isolated when
using the --pdb option.
See issue #405 for details:
https://github.com/pytest-dev/pytest-django/issues/405
"""
django_pytester.create_test_module(
'''
import os
from django.test import TestCase
from django.conf import settings
from .app.models import Item
class TestPDBIsolation(TestCase):
def setUp(self):
"""setUp should be called after starting a transaction"""
assert Item.objects.count() == 0
Item.objects.create(name='Some item')
Item.objects.create(name='Some item again')
def test_count(self):
self.assertEqual(Item.objects.count(), 2)
assert Item.objects.count() == 2
Item.objects.create(name='Foo')
self.assertEqual(Item.objects.count(), 3)
def test_count_again(self):
self.test_count()
def tearDown(self):
"""tearDown should be called before rolling back the database"""
assert Item.objects.count() == 3
'''
)
result = django_pytester.runpytest_subprocess("-v", "--pdb")
assert result.ret == 0
def test_debug_not_used(django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
"""
from django.test import TestCase
pre_setup_count = 0
class TestClass1(TestCase):
def debug(self):
assert 0, "should not be called"
def test_method(self):
pass
"""
)
result = django_pytester.runpytest_subprocess("--pdb")
result.stdout.fnmatch_lines(["*= 1 passed*"])
assert result.ret == 0
|