(pytester: Pytester)
| 666 | |
| 667 | |
| 668 | def 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 |
nothing calls this directly
no test coverage detected