Best Python code snippet using pytest-mock
test_frame.py
Source: test_frame.py
1from unittest.mock import patch2from bowling_game.frame import Frame, FrameTypes, TenthFrame3def test_frame_base_state():4 frame = Frame()5 assert frame.first_roll == 06 assert frame.second_roll == 07 assert frame.type == FrameTypes.UNPLAYED8@patch("bowling_game.frame.randint")9def test_frame_regular_play(randint_mock):10 randint_mock.return_value = 111 frame = Frame()12 frame.play()13 assert frame.first_roll == 114 assert frame.second_roll == 115 assert frame.type == FrameTypes.REGULAR16@patch("bowling_game.frame.randint")17def test_frame_spare_play(randint_mock):18 randint_mock.return_value = 519 frame = Frame()20 frame.play()21 assert frame.first_roll == 522 assert frame.second_roll == 523 assert frame.type == FrameTypes.SPARE24@patch("bowling_game.frame.randint")25def test_frame_strike_play(randint_mock):26 randint_mock.return_value = 1027 frame = Frame()28 frame.play()29 assert frame.first_roll == 1030 assert frame.second_roll == 031 assert frame.type == FrameTypes.STRIKE32def test_tenth_frame_base_state():33 frame = TenthFrame()34 assert frame.first_roll == 035 assert frame.second_roll == 036 assert frame.third_roll == 037 assert frame.type == FrameTypes.UNPLAYED38@patch("bowling_game.frame.randint")39def test_tenth_frame_regular_play(randint_mock):40 randint_mock.return_value = 141 frame = TenthFrame()42 frame.play()43 assert frame.first_roll == 144 assert frame.second_roll == 145 assert frame.third_roll == 046 assert frame.type == FrameTypes.REGULAR47@patch("bowling_game.frame.randint")48def test_tenth_frame_spare(randint_mock):49 randint_mock.return_value = 550 frame = TenthFrame()51 frame.play()52 assert frame.first_roll == 553 assert frame.second_roll == 554 assert frame.third_roll == 555 assert frame.type == FrameTypes.SPARE56@patch("bowling_game.frame.randint")57def test_tenth_frame_strike(randint_mock):58 randint_mock.side_effect = (59 10,60 4,61 2,62 )63 frame = TenthFrame()64 frame.play()65 assert frame.first_roll == 1066 assert frame.second_roll == 467 assert frame.third_roll == 268 assert frame.type == FrameTypes.STRIKE69@patch("bowling_game.frame.randint")70def test_tenth_frame_multiple_strikes(randint_mock):71 randint_mock.return_value = 1072 frame = TenthFrame()73 frame.play()74 assert frame.first_roll == 1075 assert frame.second_roll == 1076 assert frame.third_roll == 10...
tests.py
Source: tests.py
1from parameterized import parameterized2from unittest import TestCase, mock3from .lottery import count_duplicates, generate4class LotteryCountDuplicatesTestCase(TestCase):5 def test_counting_duplicates(self):6 self.assertEqual(0, count_duplicates([]))7 self.assertEqual(0, count_duplicates([1]))8 self.assertEqual(1, count_duplicates([1, 1]))9 self.assertEqual(1, count_duplicates([1, 1, 2]))10 self.assertEqual(2, count_duplicates([1, 1, 2, 2]))11 self.assertEqual(2, count_duplicates([1, 1, 2, 2, 2]))12class LotteryGenerateTestCase(TestCase):13 @mock.patch('src.lottery.randint')14 def test_only_one_value(self, randint_mock):15 randint_mock.side_effect = [1, 1, 1, 1, 1, 1, 1, 1]16 self.assertListEqual(generate(), [1, 1, 1, 1, 1, 1, 1, 1])17 self.assertEqual(8, randint_mock.call_count)18 @mock.patch('src.lottery.randint')19 def test_skip_more_duplicates(self, randint_mock):20 randint_mock.side_effect = [1, 1, 2, 2, 3, 3, 4, 5, 6]21 self.assertListEqual(22 generate(allowed_duplicates=2),23 [1, 1, 2, 2, 3, 4, 5, 6],24 )25 self.assertEqual(9, randint_mock.call_count)26 @mock.patch('src.lottery.randint')27 def test_ascending_order(self, randint_mock):28 randint_mock.side_effect = [4, 3, 2, 1]29 self.assertListEqual(generate(num=4), [1, 2, 3, 4])30 self.assertEqual(4, randint_mock.call_count)31 def test_empty_list_for_negative_num_parameter(self):32 self.assertListEqual(generate(num=-1), [])33 @parameterized.expand([34 (2, 1, 1),35 (3, 1, 2),36 (4, 1, 3),37 ])38 def test_prevent_infinity_loop(self, num, min, max):39 with self.assertRaises(AssertionError):...
Check out the latest blogs from LambdaTest on this topic:
The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.
In some sense, testing can be more difficult than coding, as validating the efficiency of the test cases (i.e., the ‘goodness’ of your tests) can be much harder than validating code correctness. In practice, the tests are just executed without any validation beyond the pass/fail verdict. On the contrary, the code is (hopefully) always validated by testing. By designing and executing the test cases the result is that some tests have passed, and some others have failed. Testers do not know much about how many bugs remain in the code, nor about their bug-revealing efficiency.
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.
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!!