Best Python code snippet using green
test_suite.py
Source:test_suite.py
...47 mock_result = MagicMock()48 mock_result._moduleSetUpFailed = True49 mock_result.shouldStop = False50 gts.run(mock_result)51 def test_failedModuleTeardown(self):52 """53 When module teardown fails, we report an error.54 """55 mock_module = MagicMock()56 mock_test = MagicMock()57 mock_err = MagicMock()58 args = copy.deepcopy(default_args)59 gts = GreenTestSuite(args=args)60 gts._get_previous_module = mock_module61 mock_result = MagicMock()62 mock_result.errors.__len__.side_effect = [0, 1, 1]63 mock_result.errors.__getitem__.side_effect = [[], [(mock_test, mock_err)]]64 mock_result._previousTestClass.__name__ = "mockClass"65 gts.run(mock_result)66 self.assertTrue(mock_test.is_class_or_module_teardown_error)67 def test_addTest_testPattern(self):68 """69 Setting test_pattern will cause a test to be filtered.70 """71 mock_test = MagicMock()72 mock_test._testMethodName = "test_hello"73 mock_test2 = MagicMock()74 mock_test2._testMethodName = "test_goodbye"75 args = copy.deepcopy(default_args)76 args.test_pattern = "_good*"77 gts = GreenTestSuite(args=args)78 gts.addTest(mock_test)79 self.assertEqual(gts._tests, [])80 gts.addTest(mock_test2)81 self.assertEqual(gts._tests, [mock_test2])82 def test_allow_stdout(self):83 """84 The allow_stdout setting should not get ignored.85 """86 class Object(object):87 pass88 args = Object()89 args.allow_stdout = True90 gts = GreenTestSuite(args=args)91 self.assertEqual(gts.allow_stdout, True)92 def test_skip_in_setUpClass(self):93 """94 If SkipTest is raised in setUpClass, then the test gets skipped95 """96 gts = GreenTestSuite(args=default_args)97 mock_test = MagicMock()98 mock_result = MagicMock()99 mock_class = MagicMock()100 mock_class.__qualname__ = "qualname"101 mock_result._previousTestClass = None102 mock_result._moduleSetUpFailed = None103 mock_result.__unittest_skip__ = None104 mock_test.__class__ = mock_class105 mock_class.setUpClass.side_effect = unittest.SkipTest("kaboom")106 gts._handleClassSetUp(mock_test, mock_result)107 self.assertTrue(mock_class.__unittest_skip__)108 self.assertEqual(mock_class.__unittest_skip_why__, "kaboom")109class TestFunctional(unittest.TestCase):110 @classmethod111 def setUpClass(cls):112 cls.startdir = os.getcwd()113 @classmethod114 def tearDownClass(cls):115 if os.getcwd() != cls.startdir:116 os.chdir(cls.startdir)117 cls.startdir = None118 def setUp(self):119 self.args = copy.deepcopy(default_args)120 self.stream = StringIO()121 self.tmpdir = tempfile.mkdtemp()122 self.loader = GreenTestLoader()123 def tearDown(self):124 del self.tmpdir125 del self.stream126 def test_skip_in_setUpClass(self):127 """128 If SkipTest is raised in setUpClass, then the test gets skipped129 """130 sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)131 fh = open(os.path.join(sub_tmpdir, "test_skipped.py"), "w")132 fh.write(133 dedent(134 """135 import unittest136 class Skipper(unittest.TestCase):137 @classmethod138 def setUpClass(cls):139 raise unittest.SkipTest("the skip reason")140 def test_one(self):141 pass142 def test_two(self):143 pass144 """145 )146 )147 fh.close()148 os.chdir(sub_tmpdir)149 tests = self.loader.loadTargets("test_skipped")150 result = run(tests, self.stream, self.args)151 os.chdir(self.startdir)152 self.assertEqual(len(result.skipped), 2)153 self.assertEqual(self.stream.getvalue().count("the skip reason"), 2)154class TestModuleTeardown(unittest.TestCase):155 @classmethod156 def setUpClass(cls):157 cls.startdir = os.getcwd()158 @classmethod159 def tearDownClass(cls):160 if os.getcwd() != cls.startdir:161 os.chdir(cls.startdir)162 cls.startdir = None163 def setUp(self):164 self.args = copy.deepcopy(default_args)165 self.stream = StringIO()166 self.tmpdir = tempfile.mkdtemp()167 self.loader = GreenTestLoader()168 def tearDown(self):169 del self.tmpdir170 del self.stream171 def test_failedModuleTeardown(self):172 """A failing tearDownModule gets counted as an errored test"""173 sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)174 fh = open(os.path.join(sub_tmpdir, "test_moduleteardownfailed.py"), "w")175 fh.write(176 dedent(177 """178 import unittest179 def tearDownModule():180 syntaxerror181 class TestRedHerring(unittest.TestCase):182 def test(self):183 pass184 """185 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!