1# -*- coding: utf-8 -*- 2from __future__ import absolute_import 3from __future__ import division 4from __future__ import print_function 5 6import gc 7 8import pytest 9from _pytest.main import EXIT_NOTESTSCOLLECTED 10 11 12def test_simple_unittest(testdir): 13 testpath = testdir.makepyfile( 14 """ 15 import unittest 16 class MyTestCase(unittest.TestCase): 17 def testpassing(self): 18 self.assertEqual('foo', 'foo') 19 def test_failing(self): 20 self.assertEqual('foo', 'bar') 21 """ 22 ) 23 reprec = testdir.inline_run(testpath) 24 assert reprec.matchreport("testpassing").passed 25 assert reprec.matchreport("test_failing").failed 26 27 28def test_runTest_method(testdir): 29 testdir.makepyfile( 30 """ 31 import unittest 32 class MyTestCaseWithRunTest(unittest.TestCase): 33 def runTest(self): 34 self.assertEqual('foo', 'foo') 35 class MyTestCaseWithoutRunTest(unittest.TestCase): 36 def runTest(self): 37 self.assertEqual('foo', 'foo') 38 def test_something(self): 39 pass 40 """ 41 ) 42 result = testdir.runpytest("-v") 43 result.stdout.fnmatch_lines( 44 """ 45 *MyTestCaseWithRunTest::runTest* 46 *MyTestCaseWithoutRunTest::test_something* 47 *2 passed* 48 """ 49 ) 50 51 52def test_isclasscheck_issue53(testdir): 53 testpath = testdir.makepyfile( 54 """ 55 import unittest 56 class _E(object): 57 def __getattr__(self, tag): 58 pass 59 E = _E() 60 """ 61 ) 62 result = testdir.runpytest(testpath) 63 assert result.ret == EXIT_NOTESTSCOLLECTED 64 65 66def test_setup(testdir): 67 testpath = testdir.makepyfile( 68 """ 69 import unittest 70 class MyTestCase(unittest.TestCase): 71 def setUp(self): 72 self.foo = 1 73 def setup_method(self, method): 74 self.foo2 = 1 75 def test_both(self): 76 self.assertEqual(1, self.foo) 77 assert self.foo2 == 1 78 def teardown_method(self, method): 79 assert 0, "42" 80 81 """ 82 ) 83 reprec = testdir.inline_run("-s", testpath) 84 assert reprec.matchreport("test_both", when="call").passed 85 rep = reprec.matchreport("test_both", when="teardown") 86 assert rep.failed and "42" in str(rep.longrepr) 87 88 89def test_setUpModule(testdir): 90 testpath = testdir.makepyfile( 91 """ 92 values = [] 93 94 def setUpModule(): 95 values.append(1) 96 97 def tearDownModule(): 98 del values[0] 99 100 def test_hello(): 101 assert values == [1] 102 103 def test_world(): 104 assert values == [1] 105 """ 106 ) 107 result = testdir.runpytest(testpath) 108 result.stdout.fnmatch_lines(["*2 passed*"]) 109 110 111def test_setUpModule_failing_no_teardown(testdir): 112 testpath = testdir.makepyfile( 113 """ 114 values = [] 115 116 def setUpModule(): 117 0/0 118 119 def tearDownModule(): 120 values.append(1) 121 122 def test_hello(): 123 pass 124 """ 125 ) 126 reprec = testdir.inline_run(testpath) 127 reprec.assertoutcome(passed=0, failed=1) 128 call = reprec.getcalls("pytest_runtest_setup")[0] 129 assert not call.item.module.values 130 131 132def test_new_instances(testdir): 133 testpath = testdir.makepyfile( 134 """ 135 import unittest 136 class MyTestCase(unittest.TestCase): 137 def test_func1(self): 138 self.x = 2 139 def test_func2(self): 140 assert not hasattr(self, 'x') 141 """ 142 ) 143 reprec = testdir.inline_run(testpath) 144 reprec.assertoutcome(passed=2) 145 146 147def test_function_item_obj_is_instance(testdir): 148 """item.obj should be a bound method on unittest.TestCase function items (#5390).""" 149 testdir.makeconftest( 150 """ 151 def pytest_runtest_makereport(item, call): 152 if call.when == 'call': 153 class_ = item.parent.obj 154 assert isinstance(item.obj.__self__, class_) 155 """ 156 ) 157 testdir.makepyfile( 158 """ 159 import unittest 160 161 class Test(unittest.TestCase): 162 def test_foo(self): 163 pass 164 """ 165 ) 166 result = testdir.runpytest_inprocess() 167 result.stdout.fnmatch_lines(["* 1 passed in*"]) 168 169 170def test_teardown(testdir): 171 testpath = testdir.makepyfile( 172 """ 173 import unittest 174 class MyTestCase(unittest.TestCase): 175 values = [] 176 def test_one(self): 177 pass 178 def tearDown(self): 179 self.values.append(None) 180 class Second(unittest.TestCase): 181 def test_check(self): 182 self.assertEqual(MyTestCase.values, [None]) 183 """ 184 ) 185 reprec = testdir.inline_run(testpath) 186 passed, skipped, failed = reprec.countoutcomes() 187 assert failed == 0, failed 188 assert passed == 2 189 assert passed + skipped + failed == 2 190 191 192def test_teardown_issue1649(testdir): 193 """ 194 Are TestCase objects cleaned up? Often unittest TestCase objects set 195 attributes that are large and expensive during setUp. 196 197 The TestCase will not be cleaned up if the test fails, because it 198 would then exist in the stackframe. 199 """ 200 testpath = testdir.makepyfile( 201 """ 202 import unittest 203 class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): 204 def setUp(self): 205 self.an_expensive_object = 1 206 def test_demo(self): 207 pass 208 209 """ 210 ) 211 testdir.inline_run("-s", testpath) 212 gc.collect() 213 for obj in gc.get_objects(): 214 assert type(obj).__name__ != "TestCaseObjectsShouldBeCleanedUp" 215 216 217def test_unittest_skip_issue148(testdir): 218 testpath = testdir.makepyfile( 219 """ 220 import unittest 221 222 @unittest.skip("hello") 223 class MyTestCase(unittest.TestCase): 224 @classmethod 225 def setUpClass(self): 226 xxx 227 def test_one(self): 228 pass 229 @classmethod 230 def tearDownClass(self): 231 xxx 232 """ 233 ) 234 reprec = testdir.inline_run(testpath) 235 reprec.assertoutcome(skipped=1) 236 237 238def test_method_and_teardown_failing_reporting(testdir): 239 testdir.makepyfile( 240 """ 241 import unittest, pytest 242 class TC(unittest.TestCase): 243 def tearDown(self): 244 assert 0, "down1" 245 def test_method(self): 246 assert False, "down2" 247 """ 248 ) 249 result = testdir.runpytest("-s") 250 assert result.ret == 1 251 result.stdout.fnmatch_lines( 252 [ 253 "*tearDown*", 254 "*assert 0*", 255 "*test_method*", 256 "*assert False*", 257 "*1 failed*1 error*", 258 ] 259 ) 260 261 262def test_setup_failure_is_shown(testdir): 263 testdir.makepyfile( 264 """ 265 import unittest 266 import pytest 267 class TC(unittest.TestCase): 268 def setUp(self): 269 assert 0, "down1" 270 def test_method(self): 271 print("never42") 272 xyz 273 """ 274 ) 275 result = testdir.runpytest("-s") 276 assert result.ret == 1 277 result.stdout.fnmatch_lines(["*setUp*", "*assert 0*down1*", "*1 failed*"]) 278 assert "never42" not in result.stdout.str() 279 280 281def test_setup_setUpClass(testdir): 282 testpath = testdir.makepyfile( 283 """ 284 import unittest 285 import pytest 286 class MyTestCase(unittest.TestCase): 287 x = 0 288 @classmethod 289 def setUpClass(cls): 290 cls.x += 1 291 def test_func1(self): 292 assert self.x == 1 293 def test_func2(self): 294 assert self.x == 1 295 @classmethod 296 def tearDownClass(cls): 297 cls.x -= 1 298 def test_teareddown(): 299 assert MyTestCase.x == 0 300 """ 301 ) 302 reprec = testdir.inline_run(testpath) 303 reprec.assertoutcome(passed=3) 304 305 306def test_setup_class(testdir): 307 testpath = testdir.makepyfile( 308 """ 309 import unittest 310 import pytest 311 class MyTestCase(unittest.TestCase): 312 x = 0 313 def setup_class(cls): 314 cls.x += 1 315 def test_func1(self): 316 assert self.x == 1 317 def test_func2(self): 318 assert self.x == 1 319 def teardown_class(cls): 320 cls.x -= 1 321 def test_teareddown(): 322 assert MyTestCase.x == 0 323 """ 324 ) 325 reprec = testdir.inline_run(testpath) 326 reprec.assertoutcome(passed=3) 327 328 329@pytest.mark.parametrize("type", ["Error", "Failure"]) 330def test_testcase_adderrorandfailure_defers(testdir, type): 331 testdir.makepyfile( 332 """ 333 from unittest import TestCase 334 import pytest 335 class MyTestCase(TestCase): 336 def run(self, result): 337 excinfo = pytest.raises(ZeroDivisionError, lambda: 0/0) 338 try: 339 result.add%s(self, excinfo._excinfo) 340 except KeyboardInterrupt: 341 raise 342 except: 343 pytest.fail("add%s should not raise") 344 def test_hello(self): 345 pass 346 """ 347 % (type, type) 348 ) 349 result = testdir.runpytest() 350 assert "should not raise" not in result.stdout.str() 351 352 353@pytest.mark.parametrize("type", ["Error", "Failure"]) 354def test_testcase_custom_exception_info(testdir, type): 355 testdir.makepyfile( 356 """ 357 from unittest import TestCase 358 import py, pytest 359 import _pytest._code 360 class MyTestCase(TestCase): 361 def run(self, result): 362 excinfo = pytest.raises(ZeroDivisionError, lambda: 0/0) 363 # we fake an incompatible exception info 364 from _pytest.monkeypatch import MonkeyPatch 365 mp = MonkeyPatch() 366 def t(*args): 367 mp.undo() 368 raise TypeError() 369 mp.setattr(_pytest._code, 'ExceptionInfo', t) 370 try: 371 excinfo = excinfo._excinfo 372 result.add%(type)s(self, excinfo) 373 finally: 374 mp.undo() 375 def test_hello(self): 376 pass 377 """ 378 % locals() 379 ) 380 result = testdir.runpytest() 381 result.stdout.fnmatch_lines( 382 [ 383 "NOTE: Incompatible Exception Representation*", 384 "*ZeroDivisionError*", 385 "*1 failed*", 386 ] 387 ) 388 389 390def test_testcase_totally_incompatible_exception_info(testdir): 391 (item,) = testdir.getitems( 392 """ 393 from unittest import TestCase 394 class MyTestCase(TestCase): 395 def test_hello(self): 396 pass 397 """ 398 ) 399 item.addError(None, 42) 400 excinfo = item._excinfo.pop(0) 401 assert "ERROR: Unknown Incompatible" in str(excinfo.getrepr()) 402 403 404def test_module_level_pytestmark(testdir): 405 testpath = testdir.makepyfile( 406 """ 407 import unittest 408 import pytest 409 pytestmark = pytest.mark.xfail 410 class MyTestCase(unittest.TestCase): 411 def test_func1(self): 412 assert 0 413 """ 414 ) 415 reprec = testdir.inline_run(testpath, "-s") 416 reprec.assertoutcome(skipped=1) 417 418 419class TestTrialUnittest(object): 420 def setup_class(cls): 421 cls.ut = pytest.importorskip("twisted.trial.unittest") 422 # on windows trial uses a socket for a reactor and apparently doesn't close it properly 423 # https://twistedmatrix.com/trac/ticket/9227 424 cls.ignore_unclosed_socket_warning = ("-W", "always") 425 426 def test_trial_testcase_runtest_not_collected(self, testdir): 427 testdir.makepyfile( 428 """ 429 from twisted.trial.unittest import TestCase 430 431 class TC(TestCase): 432 def test_hello(self): 433 pass 434 """ 435 ) 436 reprec = testdir.inline_run(*self.ignore_unclosed_socket_warning) 437 reprec.assertoutcome(passed=1) 438 testdir.makepyfile( 439 """ 440 from twisted.trial.unittest import TestCase 441 442 class TC(TestCase): 443 def runTest(self): 444 pass 445 """ 446 ) 447 reprec = testdir.inline_run(*self.ignore_unclosed_socket_warning) 448 reprec.assertoutcome(passed=1) 449 450 def test_trial_exceptions_with_skips(self, testdir): 451 testdir.makepyfile( 452 """ 453 from twisted.trial import unittest 454 import pytest 455 class TC(unittest.TestCase): 456 def test_hello(self): 457 pytest.skip("skip_in_method") 458 @pytest.mark.skipif("sys.version_info != 1") 459 def test_hello2(self): 460 pass 461 @pytest.mark.xfail(reason="iwanto") 462 def test_hello3(self): 463 assert 0 464 def test_hello4(self): 465 pytest.xfail("i2wanto") 466 def test_trial_skip(self): 467 pass 468 test_trial_skip.skip = "trialselfskip" 469 470 def test_trial_todo(self): 471 assert 0 472 test_trial_todo.todo = "mytodo" 473 474 def test_trial_todo_success(self): 475 pass 476 test_trial_todo_success.todo = "mytodo" 477 478 class TC2(unittest.TestCase): 479 def setup_class(cls): 480 pytest.skip("skip_in_setup_class") 481 def test_method(self): 482 pass 483 """ 484 ) 485 from _pytest.compat import _is_unittest_unexpected_success_a_failure 486 487 should_fail = _is_unittest_unexpected_success_a_failure() 488 result = testdir.runpytest("-rxs", *self.ignore_unclosed_socket_warning) 489 result.stdout.fnmatch_lines_random( 490 [ 491 "*XFAIL*test_trial_todo*", 492 "*trialselfskip*", 493 "*skip_in_setup_class*", 494 "*iwanto*", 495 "*i2wanto*", 496 "*sys.version_info*", 497 "*skip_in_method*", 498 "*1 failed*4 skipped*3 xfailed*" 499 if should_fail 500 else "*4 skipped*3 xfail*1 xpass*", 501 ] 502 ) 503 assert result.ret == (1 if should_fail else 0) 504 505 def test_trial_error(self, testdir): 506 testdir.makepyfile( 507 """ 508 from twisted.trial.unittest import TestCase 509 from twisted.internet.defer import Deferred 510 from twisted.internet import reactor 511 512 class TC(TestCase): 513 def test_one(self): 514 crash 515 516 def test_two(self): 517 def f(_): 518 crash 519 520 d = Deferred() 521 d.addCallback(f) 522 reactor.callLater(0.3, d.callback, None) 523 return d 524 525 def test_three(self): 526 def f(): 527 pass # will never get called 528 reactor.callLater(0.3, f) 529 # will crash at teardown 530 531 def test_four(self): 532 def f(_): 533 reactor.callLater(0.3, f) 534 crash 535 536 d = Deferred() 537 d.addCallback(f) 538 reactor.callLater(0.3, d.callback, None) 539 return d 540 # will crash both at test time and at teardown 541 """ 542 ) 543 result = testdir.runpytest() 544 result.stdout.fnmatch_lines( 545 [ 546 "*ERRORS*", 547 "*DelayedCalls*", 548 "*test_four*", 549 "*NameError*crash*", 550 "*test_one*", 551 "*NameError*crash*", 552 "*test_three*", 553 "*DelayedCalls*", 554 "*test_two*", 555 "*crash*", 556 ] 557 ) 558 559 def test_trial_pdb(self, testdir): 560 p = testdir.makepyfile( 561 """ 562 from twisted.trial import unittest 563 import pytest 564 class TC(unittest.TestCase): 565 def test_hello(self): 566 assert 0, "hellopdb" 567 """ 568 ) 569 child = testdir.spawn_pytest(p) 570 child.expect("hellopdb") 571 child.sendeof() 572 573 def test_trial_testcase_skip_property(self, testdir): 574 testpath = testdir.makepyfile( 575 """ 576 from twisted.trial import unittest 577 class MyTestCase(unittest.TestCase): 578 skip = 'dont run' 579 def test_func(self): 580 pass 581 """ 582 ) 583 reprec = testdir.inline_run(testpath, "-s") 584 reprec.assertoutcome(skipped=1) 585 586 def test_trial_testfunction_skip_property(self, testdir): 587 testpath = testdir.makepyfile( 588 """ 589 from twisted.trial import unittest 590 class MyTestCase(unittest.TestCase): 591 def test_func(self): 592 pass 593 test_func.skip = 'dont run' 594 """ 595 ) 596 reprec = testdir.inline_run(testpath, "-s") 597 reprec.assertoutcome(skipped=1) 598 599 def test_trial_testcase_todo_property(self, testdir): 600 testpath = testdir.makepyfile( 601 """ 602 from twisted.trial import unittest 603 class MyTestCase(unittest.TestCase): 604 todo = 'dont run' 605 def test_func(self): 606 assert 0 607 """ 608 ) 609 reprec = testdir.inline_run(testpath, "-s") 610 reprec.assertoutcome(skipped=1) 611 612 def test_trial_testfunction_todo_property(self, testdir): 613 testpath = testdir.makepyfile( 614 """ 615 from twisted.trial import unittest 616 class MyTestCase(unittest.TestCase): 617 def test_func(self): 618 assert 0 619 test_func.todo = 'dont run' 620 """ 621 ) 622 reprec = testdir.inline_run( 623 testpath, "-s", *self.ignore_unclosed_socket_warning 624 ) 625 reprec.assertoutcome(skipped=1) 626 627 628def test_djangolike_testcase(testdir): 629 # contributed from Morten Breekevold 630 testdir.makepyfile( 631 """ 632 from unittest import TestCase, main 633 634 class DjangoLikeTestCase(TestCase): 635 636 def setUp(self): 637 print("setUp()") 638 639 def test_presetup_has_been_run(self): 640 print("test_thing()") 641 self.assertTrue(hasattr(self, 'was_presetup')) 642 643 def tearDown(self): 644 print("tearDown()") 645 646 def __call__(self, result=None): 647 try: 648 self._pre_setup() 649 except (KeyboardInterrupt, SystemExit): 650 raise 651 except Exception: 652 import sys 653 result.addError(self, sys.exc_info()) 654 return 655 super(DjangoLikeTestCase, self).__call__(result) 656 try: 657 self._post_teardown() 658 except (KeyboardInterrupt, SystemExit): 659 raise 660 except Exception: 661 import sys 662 result.addError(self, sys.exc_info()) 663 return 664 665 def _pre_setup(self): 666 print("_pre_setup()") 667 self.was_presetup = True 668 669 def _post_teardown(self): 670 print("_post_teardown()") 671 """ 672 ) 673 result = testdir.runpytest("-s") 674 assert result.ret == 0 675 result.stdout.fnmatch_lines( 676 [ 677 "*_pre_setup()*", 678 "*setUp()*", 679 "*test_thing()*", 680 "*tearDown()*", 681 "*_post_teardown()*", 682 ] 683 ) 684 685 686def test_unittest_not_shown_in_traceback(testdir): 687 testdir.makepyfile( 688 """ 689 import unittest 690 class t(unittest.TestCase): 691 def test_hello(self): 692 x = 3 693 self.assertEqual(x, 4) 694 """ 695 ) 696 res = testdir.runpytest() 697 assert "failUnlessEqual" not in res.stdout.str() 698 699 700def test_unorderable_types(testdir): 701 testdir.makepyfile( 702 """ 703 import unittest 704 class TestJoinEmpty(unittest.TestCase): 705 pass 706 707 def make_test(): 708 class Test(unittest.TestCase): 709 pass 710 Test.__name__ = "TestFoo" 711 return Test 712 TestFoo = make_test() 713 """ 714 ) 715 result = testdir.runpytest() 716 assert "TypeError" not in result.stdout.str() 717 assert result.ret == EXIT_NOTESTSCOLLECTED 718 719 720def test_unittest_typerror_traceback(testdir): 721 testdir.makepyfile( 722 """ 723 import unittest 724 class TestJoinEmpty(unittest.TestCase): 725 def test_hello(self, arg1): 726 pass 727 """ 728 ) 729 result = testdir.runpytest() 730 assert "TypeError" in result.stdout.str() 731 assert result.ret == 1 732 733 734@pytest.mark.parametrize("runner", ["pytest", "unittest"]) 735def test_unittest_expected_failure_for_failing_test_is_xfail(testdir, runner): 736 script = testdir.makepyfile( 737 """ 738 import unittest 739 class MyTestCase(unittest.TestCase): 740 @unittest.expectedFailure 741 def test_failing_test_is_xfail(self): 742 assert False 743 if __name__ == '__main__': 744 unittest.main() 745 """ 746 ) 747 if runner == "pytest": 748 result = testdir.runpytest("-rxX") 749 result.stdout.fnmatch_lines( 750 ["*XFAIL*MyTestCase*test_failing_test_is_xfail*", "*1 xfailed*"] 751 ) 752 else: 753 result = testdir.runpython(script) 754 result.stderr.fnmatch_lines(["*1 test in*", "*OK*(expected failures=1)*"]) 755 assert result.ret == 0 756 757 758@pytest.mark.parametrize("runner", ["pytest", "unittest"]) 759def test_unittest_expected_failure_for_passing_test_is_fail(testdir, runner): 760 script = testdir.makepyfile( 761 """ 762 import unittest 763 class MyTestCase(unittest.TestCase): 764 @unittest.expectedFailure 765 def test_passing_test_is_fail(self): 766 assert True 767 if __name__ == '__main__': 768 unittest.main() 769 """ 770 ) 771 from _pytest.compat import _is_unittest_unexpected_success_a_failure 772 773 should_fail = _is_unittest_unexpected_success_a_failure() 774 if runner == "pytest": 775 result = testdir.runpytest("-rxX") 776 result.stdout.fnmatch_lines( 777 [ 778 "*MyTestCase*test_passing_test_is_fail*", 779 "*1 failed*" if should_fail else "*1 xpassed*", 780 ] 781 ) 782 else: 783 result = testdir.runpython(script) 784 result.stderr.fnmatch_lines(["*1 test in*", "*(unexpected successes=1)*"]) 785 786 assert result.ret == (1 if should_fail else 0) 787 788 789@pytest.mark.parametrize( 790 "fix_type, stmt", [("fixture", "return"), ("yield_fixture", "yield")] 791) 792def test_unittest_setup_interaction(testdir, fix_type, stmt): 793 testdir.makepyfile( 794 """ 795 import unittest 796 import pytest 797 class MyTestCase(unittest.TestCase): 798 @pytest.{fix_type}(scope="class", autouse=True) 799 def perclass(self, request): 800 request.cls.hello = "world" 801 {stmt} 802 @pytest.{fix_type}(scope="function", autouse=True) 803 def perfunction(self, request): 804 request.instance.funcname = request.function.__name__ 805 {stmt} 806 807 def test_method1(self): 808 assert self.funcname == "test_method1" 809 assert self.hello == "world" 810 811 def test_method2(self): 812 assert self.funcname == "test_method2" 813 814 def test_classattr(self): 815 assert self.__class__.hello == "world" 816 """.format( 817 fix_type=fix_type, stmt=stmt 818 ) 819 ) 820 result = testdir.runpytest() 821 result.stdout.fnmatch_lines(["*3 passed*"]) 822 823 824def test_non_unittest_no_setupclass_support(testdir): 825 testpath = testdir.makepyfile( 826 """ 827 class TestFoo(object): 828 x = 0 829 830 @classmethod 831 def setUpClass(cls): 832 cls.x = 1 833 834 def test_method1(self): 835 assert self.x == 0 836 837 @classmethod 838 def tearDownClass(cls): 839 cls.x = 1 840 841 def test_not_teareddown(): 842 assert TestFoo.x == 0 843 844 """ 845 ) 846 reprec = testdir.inline_run(testpath) 847 reprec.assertoutcome(passed=2) 848 849 850def test_no_teardown_if_setupclass_failed(testdir): 851 testpath = testdir.makepyfile( 852 """ 853 import unittest 854 855 class MyTestCase(unittest.TestCase): 856 x = 0 857 858 @classmethod 859 def setUpClass(cls): 860 cls.x = 1 861 assert False 862 863 def test_func1(self): 864 cls.x = 10 865 866 @classmethod 867 def tearDownClass(cls): 868 cls.x = 100 869 870 def test_notTornDown(): 871 assert MyTestCase.x == 1 872 """ 873 ) 874 reprec = testdir.inline_run(testpath) 875 reprec.assertoutcome(passed=1, failed=1) 876 877 878def test_issue333_result_clearing(testdir): 879 testdir.makeconftest( 880 """ 881 import pytest 882 @pytest.hookimpl(hookwrapper=True) 883 def pytest_runtest_call(item): 884 yield 885 assert 0 886 """ 887 ) 888 testdir.makepyfile( 889 """ 890 import unittest 891 class TestIt(unittest.TestCase): 892 def test_func(self): 893 0/0 894 """ 895 ) 896 897 reprec = testdir.inline_run() 898 reprec.assertoutcome(failed=1) 899 900 901def test_unittest_raise_skip_issue748(testdir): 902 testdir.makepyfile( 903 test_foo=""" 904 import unittest 905 906 class MyTestCase(unittest.TestCase): 907 def test_one(self): 908 raise unittest.SkipTest('skipping due to reasons') 909 """ 910 ) 911 result = testdir.runpytest("-v", "-rs") 912 result.stdout.fnmatch_lines( 913 """ 914 *SKIP*[1]*test_foo.py*skipping due to reasons* 915 *1 skipped* 916 """ 917 ) 918 919 920def test_unittest_skip_issue1169(testdir): 921 testdir.makepyfile( 922 test_foo=""" 923 import unittest 924 925 class MyTestCase(unittest.TestCase): 926 @unittest.skip("skipping due to reasons") 927 def test_skip(self): 928 self.fail() 929 """ 930 ) 931 result = testdir.runpytest("-v", "-rs") 932 result.stdout.fnmatch_lines( 933 """ 934 *SKIP*[1]*skipping due to reasons* 935 *1 skipped* 936 """ 937 ) 938 939 940def test_class_method_containing_test_issue1558(testdir): 941 testdir.makepyfile( 942 test_foo=""" 943 import unittest 944 945 class MyTestCase(unittest.TestCase): 946 def test_should_run(self): 947 pass 948 def test_should_not_run(self): 949 pass 950 test_should_not_run.__test__ = False 951 """ 952 ) 953 reprec = testdir.inline_run() 954 reprec.assertoutcome(passed=1) 955 956 957@pytest.mark.parametrize( 958 "base", ["six.moves.builtins.object", "unittest.TestCase", "unittest2.TestCase"] 959) 960def test_usefixtures_marker_on_unittest(base, testdir): 961 """#3498""" 962 module = base.rsplit(".", 1)[0] 963 pytest.importorskip(module) 964 testdir.makepyfile( 965 conftest=""" 966 import pytest 967 968 @pytest.fixture(scope='function') 969 def fixture1(request, monkeypatch): 970 monkeypatch.setattr(request.instance, 'fixture1', True ) 971 972 973 @pytest.fixture(scope='function') 974 def fixture2(request, monkeypatch): 975 monkeypatch.setattr(request.instance, 'fixture2', True ) 976 977 def node_and_marks(item): 978 print(item.nodeid) 979 for mark in item.iter_markers(): 980 print(" ", mark) 981 982 @pytest.fixture(autouse=True) 983 def my_marks(request): 984 node_and_marks(request.node) 985 986 def pytest_collection_modifyitems(items): 987 for item in items: 988 node_and_marks(item) 989 990 """ 991 ) 992 993 testdir.makepyfile( 994 """ 995 import pytest 996 import {module} 997 998 class Tests({base}): 999 fixture1 = False 1000 fixture2 = False 1001 1002 @pytest.mark.usefixtures("fixture1") 1003 def test_one(self): 1004 assert self.fixture1 1005 assert not self.fixture2 1006 1007 @pytest.mark.usefixtures("fixture1", "fixture2") 1008 def test_two(self): 1009 assert self.fixture1 1010 assert self.fixture2 1011 1012 1013 """.format( 1014 module=module, base=base 1015 ) 1016 ) 1017 1018 result = testdir.runpytest("-s") 1019 result.assert_outcomes(passed=2) 1020 1021 1022def test_testcase_handles_init_exceptions(testdir): 1023 """ 1024 Regression test to make sure exceptions in the __init__ method are bubbled up correctly. 1025 See https://github.com/pytest-dev/pytest/issues/3788 1026 """ 1027 testdir.makepyfile( 1028 """ 1029 from unittest import TestCase 1030 import pytest 1031 class MyTestCase(TestCase): 1032 def __init__(self, *args, **kwargs): 1033 raise Exception("should raise this exception") 1034 def test_hello(self): 1035 pass 1036 """ 1037 ) 1038 result = testdir.runpytest() 1039 assert "should raise this exception" in result.stdout.str() 1040 assert "ERROR at teardown of MyTestCase.test_hello" not in result.stdout.str() 1041 1042 1043def test_error_message_with_parametrized_fixtures(testdir): 1044 testdir.copy_example("unittest/test_parametrized_fixture_error_message.py") 1045 result = testdir.runpytest() 1046 result.stdout.fnmatch_lines( 1047 [ 1048 "*test_two does not support fixtures*", 1049 "*TestSomethingElse::test_two", 1050 "*Function type: TestCaseFunction", 1051 ] 1052 ) 1053 1054 1055@pytest.mark.parametrize( 1056 "test_name, expected_outcome", 1057 [ 1058 ("test_setup_skip.py", "1 skipped"), 1059 ("test_setup_skip_class.py", "1 skipped"), 1060 ("test_setup_skip_module.py", "1 error"), 1061 ], 1062) 1063def test_setup_inheritance_skipping(testdir, test_name, expected_outcome): 1064 """Issue #4700""" 1065 testdir.copy_example("unittest/{}".format(test_name)) 1066 result = testdir.runpytest() 1067 result.stdout.fnmatch_lines(["* {} in *".format(expected_outcome)]) 1068