def test_2(self): tester = main.Tester() for i in range(100): tester.add_to_all(str(i), i) for i in range(0, 100): assert tester.tree_dict.get(str(i)) == i assert tester.sorted_array_dict.get(str(i)) == i assert tester.array_dict.get(str(i)) == i assert tester.hash_dict.get(str(i)) == i assert tester.treap_dict.get(str(i)) == i
def test_3(self): tester = main.Tester() for i in range(100): tester.add_to_all(str(i), i) for i in range(0, 100): tester.remove_from_all(str(i)) for i in range(0, 100): assert tester.tree_dict.get(str(i)) is None assert tester.sorted_array_dict.get(str(i)) is None assert tester.array_dict.get(str(i)) is None assert tester.hash_dict.get(str(i)) is None assert tester.treap_dict.get(str(i)) is None
def test_one(self): # Create an instance of the Testing class - we could fully mock this instead of creating an instance of Testing, but this is nicer test = main.Testing() # Expected return value for stubbed method expectedOne = "thisIsOne" expectedTwo = "thisIsTwo" expectedThree = "thisIsThree" when(main.Testing).doSomething("one").thenReturn(expectedOne) when(main.Testing).doSomething("two").thenReturn(expectedTwo) when(main.Testing).doSomething("three").thenReturn(expectedThree) # Simulate an error for this specific input - This might trigger function error-handling logic when(main.Testing).doSomething("four").thenRaise(ValueError) # Call doSomething and verify that we got the expected output resultOne = test.doSomething("one") self.assertEqual(expectedOne, resultOne) # Call doSomething and verify that we got the expected output resultTwo = test.doSomething("two") self.assertEqual(expectedTwo, resultTwo) # call Tester.run() and verify that we got the expected output resultThree = main.Tester().run(test, "three") self.assertEqual(expectedThree, resultThree) # Since we're learning, let's see how to verify a function raises an exception we expect it to with self.assertRaises(ValueError): print(test.doSomething("four")) # Verify that we only called doSomething 4 times (once from Tester().run()). # Generally, you would use this to ensure the function you passed your mock into called the mocked # function the correct number of times # (ex: perhaps the function is called once for each object in a list) # You can change the value passed to 'times' to see a failure # We pass 'ANY' to doSomething to verify that, overall, the function was called 4 times. # We could pass in a specific value for the parameter and verify how many times it was called with a give parameter value verify(main.Testing, times=4).doSomething(ANY) # Uncomment to cause a test failure due to calling with an unstubbed parameter #test.doSomething("five") # Mock the plain method inside of main.py expectedGoogleHtml = "<html>This Is Google</html>" when(main).plainFunction("google.com").thenReturn(expectedGoogleHtml) self.assertEqual(expectedGoogleHtml, main.plainFunction("google.com")) # remove all stubs - This may not be needed since we stub within a single function, but I haven't tested unstub()
def test_6(self): tester = main.Tester() array = [] for i in range(300): rnd = str(random.randint(0, 1e9)) array.append(rnd) tester.add_to_all(rnd, int(rnd)) for i in range(0, 300): if i % 2 == 0: continue tester.remove_from_all(array[i]) for i in range(0, 300): if i % 2 == 0: continue assert tester.tree_dict.get(array[i]) is None assert tester.sorted_array_dict.get(array[i]) is None assert tester.array_dict.get(array[i]) is None assert tester.hash_dict.get(array[i]) is None assert tester.treap_dict.get(array[i]) is None