MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_djangolike_testcase

Function test_djangolike_testcase

testing/test_unittest.py:668–723  ·  view source on GitHub ↗
(pytester: Pytester)

Source from the content-addressed store, hash-verified

666
667
668def test_djangolike_testcase(pytester: Pytester) -> None:
669 # contributed from Morten Breekevold
670 pytester.makepyfile(
671 """
672 from unittest import TestCase, main
673
674 class DjangoLikeTestCase(TestCase):
675
676 def setUp(self):
677 print("setUp()")
678
679 def test_presetup_has_been_run(self):
680 print("test_thing()")
681 self.assertTrue(hasattr(self, 'was_presetup'))
682
683 def tearDown(self):
684 print("tearDown()")
685
686 def __call__(self, result=None):
687 try:
688 self._pre_setup()
689 except (KeyboardInterrupt, SystemExit):
690 raise
691 except Exception:
692 import sys
693 result.addError(self, sys.exc_info())
694 return
695 super(DjangoLikeTestCase, self).__call__(result)
696 try:
697 self._post_teardown()
698 except (KeyboardInterrupt, SystemExit):
699 raise
700 except Exception:
701 import sys
702 result.addError(self, sys.exc_info())
703 return
704
705 def _pre_setup(self):
706 print("_pre_setup()")
707 self.was_presetup = True
708
709 def _post_teardown(self):
710 print("_post_teardown()")
711 """
712 )
713 result = pytester.runpytest("-s")
714 assert result.ret == 0
715 result.stdout.fnmatch_lines(
716 [
717 "*_pre_setup()*",
718 "*setUp()*",
719 "*test_thing()*",
720 "*tearDown()*",
721 "*_post_teardown()*",
722 ]
723 )
724
725

Callers

nothing calls this directly

Calls 3

fnmatch_linesMethod · 0.80
makepyfileMethod · 0.45
runpytestMethod · 0.45

Tested by

no test coverage detected