Best Python code snippet using pytest-mock
test_method_multiple_signatures.py
1from __future__ import print_function2from __future__ import division3from __future__ import absolute_import4import unittest5import sys6from jnius.reflect import autoclass7try:8 long9except NameError:10 # Python 311 long = int12class MultipleSignature(unittest.TestCase):13 def test_multiple_constructors(self):14 String = autoclass('java.lang.String')15 self.assertIsNotNone(String('Hello World'))16 self.assertIsNotNone(String(list('Hello World')))17 self.assertIsNotNone(String(list('Hello World'), 3, 5))18 def test_multiple_methods(self):19 String = autoclass('java.lang.String')20 s = String('hello')21 if sys.version_info >= (3, 0):22 self.assertEqual(s.getBytes(), [104, 101, 108, 108, 111])23 self.assertEqual(s.getBytes('utf8'), [104, 101, 108, 108, 111])24 self.assertEqual(s.indexOf(ord('e')), 1)25 self.assertEqual(s.indexOf(ord('e'), 2), -1)26 def test_multiple_methods_no_args(self):27 MultipleMethods = autoclass('org.jnius.MultipleMethods')28 self.assertEqual(MultipleMethods.resolve(), 'resolved no args')29 def test_multiple_methods_one_arg(self):30 MultipleMethods = autoclass('org.jnius.MultipleMethods')31 self.assertEqual(MultipleMethods.resolve('arg'), 'resolved one arg')32 def test_multiple_methods_two_args(self):33 MultipleMethods = autoclass('org.jnius.MultipleMethods')34 self.assertEqual(MultipleMethods.resolve('one', 'two'), 'resolved two args')35 def test_multiple_methods_two_string_and_an_integer(self):36 MultipleMethods = autoclass('org.jnius.MultipleMethods')37 self.assertEqual(MultipleMethods.resolve('one', 'two', 1), 'resolved two string and an integer')38 def test_multiple_methods_two_string_and_two_integers(self):39 MultipleMethods = autoclass('org.jnius.MultipleMethods')40 self.assertEqual(MultipleMethods.resolve('one', 'two', 1, 2), 'resolved two string and two integers')41 def test_multiple_methods_varargs(self):42 MultipleMethods = autoclass('org.jnius.MultipleMethods')43 self.assertEqual(MultipleMethods.resolve(1, 2, 3), 'resolved varargs')44 def test_multiple_methods_varargs_long(self):45 MultipleMethods = autoclass('org.jnius.MultipleMethods')46 self.assertEqual(MultipleMethods.resolve(long(1), long(2), long(3)), 'resolved varargs')47 def test_multiple_methods_two_args_and_varargs(self):48 MultipleMethods = autoclass('org.jnius.MultipleMethods')49 self.assertEqual(MultipleMethods.resolve('one', 'two', 1, 2, 3), 'resolved two args and varargs')50 def test_multiple_methods_one_int_one_small_long_and_a_string(self):51 MultipleMethods = autoclass('org.jnius.MultipleMethods')52 self.assertEqual(MultipleMethods.resolve(53 1, long(1), "one"), "resolved one int, one long and a string")54 def test_multiple_methods_one_int_one_actual_long_and_a_string(self):55 MultipleMethods = autoclass('org.jnius.MultipleMethods')56 self.assertEqual(MultipleMethods.resolve(...
least_common_multiple.py
Source: least_common_multiple.py
1import unittest2from timeit import timeit3def least_common_multiple_slow(first_num: int, second_num: int) -> int:4 """5 Find the least common multiple of two numbers.6 Learn more: https://en.wikipedia.org/wiki/Least_common_multiple7 >>> least_common_multiple_slow(5, 2)8 109 >>> least_common_multiple_slow(12, 76)10 22811 """12 max_num = first_num if first_num >= second_num else second_num13 common_mult = max_num14 while (common_mult % first_num > 0) or (common_mult % second_num > 0):15 common_mult += max_num16 return common_mult17def greatest_common_divisor(a: int, b: int) -> int:18 """19 Calculate Greatest Common Divisor (GCD).20 see greatest_common_divisor.py21 >>> greatest_common_divisor(24, 40)22 823 >>> greatest_common_divisor(1, 1)24 125 >>> greatest_common_divisor(1, 800)26 127 >>> greatest_common_divisor(11, 37)28 129 >>> greatest_common_divisor(3, 5)30 131 >>> greatest_common_divisor(16, 4)32 433 """34 return b if a == 0 else greatest_common_divisor(b % a, a)35def least_common_multiple_fast(first_num: int, second_num: int) -> int:36 """37 Find the least common multiple of two numbers.38 https://en.wikipedia.org/wiki/Least_common_multiple#Using_the_greatest_common_divisor39 >>> least_common_multiple_fast(5,2)40 1041 >>> least_common_multiple_fast(12,76)42 22843 """44 return first_num // greatest_common_divisor(first_num, second_num) * second_num45def benchmark():46 setup = (47 "from __main__ import least_common_multiple_slow, least_common_multiple_fast"48 )49 print(50 "least_common_multiple_slow():",51 timeit("least_common_multiple_slow(1000, 999)", setup=setup),52 )53 print(54 "least_common_multiple_fast():",55 timeit("least_common_multiple_fast(1000, 999)", setup=setup),56 )57class TestLeastCommonMultiple(unittest.TestCase):58 test_inputs = [59 (10, 20),60 (13, 15),61 (4, 31),62 (10, 42),63 (43, 34),64 (5, 12),65 (12, 25),66 (10, 25),67 (6, 9),68 ]69 expected_results = [20, 195, 124, 210, 1462, 60, 300, 50, 18]70 def test_lcm_function(self):71 for i, (first_num, second_num) in enumerate(self.test_inputs):72 slow_result = least_common_multiple_slow(first_num, second_num)73 fast_result = least_common_multiple_fast(first_num, second_num)74 with self.subTest(i=i):75 self.assertEqual(slow_result, self.expected_results[i])76 self.assertEqual(fast_result, self.expected_results[i])77if __name__ == "__main__":78 benchmark()...
test_fizzbuzz.py
Source: test_fizzbuzz.py
1from pytest import mark23from src.fizzbuzz import fizzbuzz456@mark.fizzbuzz7@mark.parametrize(8 'multiple_of_only_3',9 [i for i in range(0, 100, 3) if i % 5 != 0]10)11def test_fizzbuzz_should_return_fizz(multiple_of_only_3: int):12 assert fizzbuzz(multiple_of_only_3) == 'fizz'131415@mark.fizzbuzz16@mark.parametrize(17 'multiple_of_only_5',18 [i for i in range(0, 100, 5) if i % 3 != 0]19)20def test_fizzbuzz_should_return_buzz(multiple_of_only_5: int):21 assert fizzbuzz(multiple_of_only_5) == 'buzz'222324@mark.fizzbuzz25@mark.parametrize(26 'multiple_of_both_3_and_5',27 [i for i in range(0, 100, 3) if i % 5 == 0]28)29def test_fizzbuzz_should_return_fizzbuzz(multiple_of_both_3_and_5: int):30 assert fizzbuzz(multiple_of_both_3_and_5) == 'fizzbuzz'313233@mark.fizzbuzz34@mark.parametrize(35 'neither_multiple_of_3_nor_5',36 [i for i in range(0, 100) if i % 3 != 0 and i % 5 != 0]37)38def test_fizzbuzz_should_return_None(neither_multiple_of_3_nor_5: int):39 assert fizzbuzz(neither_multiple_of_3_nor_5) == None404142@mark.fizzbuzz43def test_when_fizzbuzz_receives_multiple_of_only_3_then_should_return_fizz():44 assert fizzbuzz(6) == 'fizz'454647@mark.fizzbuzz48def test_when_fizzbuzz_receives_multiple_of_only_5_then_should_return_buzz():49 assert fizzbuzz(10) == 'buzz'505152@mark.fizzbuzz53def test_when_fizzbuzz_receives_multiple_of_both_3_and_5_then_should_return_fizzbuzz():54 assert fizzbuzz(15) == 'fizzbuzz'555657@mark.fizzbuzz58def test_when_fizzbuzz_do_not_receive_multiple_of_3_or_5_then_should_return_None():59 assert fizzbuzz(4) == None
...
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!!