Best Python code snippet using locust
testrunner_test.py
Source:testrunner_test.py
1import unittest2from runner import TestRunner3import pmock4class TestRunnerTest(unittest.TestCase):5 6 def setUp(self):7 self.test_url = TestRunner.TEST_URL8 self.test_webserver = pmock.Mock()9 self.browser_launcher = pmock.Mock()10 self.browser_launcher.stubs().type().will(pmock.return_value("launcher"))11 self.browser_launcher.stubs().killAllInstances()12 def tearDown(self):13 self.test_webserver.verify()14 self.browser_launcher.verify()15 16 17 def testRunTestsLaunchesTestForEachBrowser(self):18 browser_launchers = [pmock.Mock(), pmock.Mock()]19 self.test_webserver.expects(pmock.once()).startServing()20 expected_results = {}21 launcher_id = 022 for browser_launcher in browser_launchers:23 browser_launcher.stubs().type() \24 .will(pmock.return_value(str(launcher_id)))25 expected_results[browser_launcher.type()] = "TIMED-OUT"26 launcher_id += 127 for browser_launcher in browser_launchers:28 self.test_webserver.expects(pmock.once()) \29 .startTest(pmock.eq(TestRunner.TIMEOUT)) \30 .will(pmock.return_value(expected_results[browser_launcher.type()]))31 browser_launcher.expects(pmock.once()) \32 .launch(pmock.eq(TestRunner.TEST_URL))33 self.test_webserver.expects(pmock.once()).testResults() \34 .will(pmock.return_value(expected_results[browser_launcher.type()]))35 browser_launcher.expects(pmock.once()).killAllInstances()36 37 self.test_webserver.expects(pmock.once()).shutdown()38 39 aggregated_results = TestRunner(browser_launchers, 40 self.test_webserver).runTests()41 self.assertEqual(expected_results, aggregated_results)42 43 for browser_launcher in browser_launchers:44 browser_launcher.verify()45 46 47 def testRunTestsRequiresAtLeastOneBrowserLaucher(self):48 self.assertRaises(ValueError, TestRunner, [], self.test_webserver)49 50 51 def testRunTestsWebserverLanchedBeforeBrowserInvoked(self):52 self.test_webserver.expects(pmock.once()).startServing()53 self.test_webserver.expects(pmock.once()) \54 .startTest(pmock.eq(TestRunner.TIMEOUT))55 56 self.browser_launcher.expects(pmock.once()) \57 .launch(pmock.eq(TestRunner.TEST_URL)) \58 .after("startTest", self.test_webserver)59 60 self.test_webserver.expects(pmock.once()) \61 .testResults() \62 .after("launch", self.browser_launcher)63 64 self.test_webserver.expects(pmock.once()).shutdown()65 66 TestRunner([self.browser_launcher], self.test_webserver).runTests()67 68 69 def testOneBrowserLaunchFailureDoesNotAffectOtherBrowserTesting(self):70 self.test_webserver.expects(pmock.once()).startServing()71 self.test_webserver.expects(pmock.once()) \72 .startTest(pmock.eq(TestRunner.TIMEOUT))73 self.test_webserver.expects(pmock.once()) \74 .startTest(pmock.eq(TestRunner.TIMEOUT))75 76 77 failing_browser_launcher = pmock.Mock()78 failing_browser_launcher.stubs().type() \79 .will(pmock.return_value("launcher1"))80 failing_browser_launcher.expects(pmock.once()) \81 .launch(pmock.eq(TestRunner.TEST_URL)) \82 .will(pmock.raise_exception(RuntimeError("browser lauch failed")))83 failing_browser_launcher.expects(pmock.once()).killAllInstances()84 85 self.test_webserver.expects(pmock.once()) \86 .testResults() \87 .after("launch", failing_browser_launcher)88 89 second_browser_launcher = pmock.Mock()90 second_browser_launcher.stubs().type() \91 .will(pmock.return_value("launcher2"))92 second_browser_launcher.expects(pmock.once()) \93 .launch(pmock.eq(TestRunner.TEST_URL)) 94 second_browser_launcher.expects(pmock.once()).killAllInstances()95 96 self.test_webserver.expects(pmock.once()) \97 .testResults() \98 .after("launch", second_browser_launcher)99 100 101 self.test_webserver.expects(pmock.once()).shutdown()102 103 TestRunner([failing_browser_launcher, second_browser_launcher], 104 self.test_webserver).runTests()105 failing_browser_launcher.verify()106 second_browser_launcher.verify()107 108 def testOneBrowserKillFailureDoesNotAffectOtherBrowserTesting(self):109 self.test_webserver.expects(pmock.once()).startServing()110 self.test_webserver.expects(pmock.once()) \111 .startTest(pmock.eq(TestRunner.TIMEOUT))112 self.test_webserver.expects(pmock.once()) \113 .startTest(pmock.eq(TestRunner.TIMEOUT))114 115 116 failing_browser_launcher = pmock.Mock()117 failing_browser_launcher.stubs().type() \118 .will(pmock.return_value("launcher1"))119 failing_browser_launcher.expects(pmock.once()) \120 .launch(pmock.eq(TestRunner.TEST_URL))121 failing_browser_launcher.expects(pmock.once()).killAllInstances() \122 .will(pmock.raise_exception(RuntimeError("browser kill failed")))123 124 self.test_webserver.expects(pmock.once()) \125 .testResults() \126 .after("launch", failing_browser_launcher)127 128 second_browser_launcher = pmock.Mock()129 second_browser_launcher.stubs().type() \130 .will(pmock.return_value("launcher2"))131 second_browser_launcher.expects(pmock.once()) \132 .launch(pmock.eq(TestRunner.TEST_URL)) 133 second_browser_launcher.expects(pmock.once()).killAllInstances()134 135 self.test_webserver.expects(pmock.once()) \136 .testResults() \137 .after("launch", second_browser_launcher)138 139 140 self.test_webserver.expects(pmock.once()).shutdown()141 142 TestRunner([failing_browser_launcher, second_browser_launcher], 143 self.test_webserver).runTests()144 failing_browser_launcher.verify()145 second_browser_launcher.verify()146 147 def testBrowserTypeMustBeUnique(self):148 browser_launcher1 = pmock.Mock()149 browser_launcher2 = pmock.Mock()150 151 duplicated_type = "same type value"152 browser_launcher1.stubs().type().will(pmock.return_value(duplicated_type))153 browser_launcher2.stubs().type().will(pmock.return_value(duplicated_type))154 155 self.assertRaises(ValueError, TestRunner, 156 [browser_launcher1, browser_launcher2], 157 self.test_webserver)158 159 160if __name__ == "__main__":161 unittest.main() ...
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!!