class TestPageRequester(TestCase): def setUp(self): self.page = PageRequester('https://google.com') def test_make_request(self): with patch('requests.get') as mocked_get: self.page.get() mocked_get.assert_called() def test_content_returned(self): class FakeResponse: def __init__(self): self.content = 'Hello' with patch('requests.get', return_value=FakeResponse()) as mocked_get: result = self.page.get() self.assertEqual(result, 'Hello')
class TestPageRequester(TestCase): def setUp(self): self.page = PageRequester('https://google.com') def test_make_request(self): # we do not need to test the get response validity but the fact that we can do a get and have a response with patch('requests.get') as mocked_get: # this will replace all response.get with a moccked response self.page.get() mocked_get.assert_called() def test_content_returned(self): # now we can also test the returned content, but still with a mocked response. class FakeResponse: # we need to declare a fix mocked response, as the default mocked one is random def __init__(self): self.content = 'Hello' with patch('requests.get', return_value=FakeResponse()) as mocked_get: result = self.page.get() self.assertEqual(result, 'Hello')
class TestPageRequester(TestCase): def setUp(self): self.page = PageRequester("http://google.com") def test_make_request(self): with patch("requests.get") as mocked_get: self.page.get() mocked_get.assert_called() def test_content_returned(self): fake_content = "Hello" class FakeResponse: def __init__(self): self.content = fake_content with patch("requests.get", return_value=FakeResponse()) as mocked_get: result = self.page.get() self.assertEqual(result, fake_content)
class TestPageRequester(TestCase): def setUp(self): self.page = PageRequester('https://google.com') def test_make_request(self): with patch( 'requests.get' ) as mocked_get: #requests module already has its test in the library self.page.get( ) #patch is replacing the content of request.get with a mock mocked_get.assert_called() def test_content_returned(self): class FakeResponse: def __init__(self): self.content = 'Hello' #this helps to test only the get method from our side with patch('requests.get', return_value=FakeResponse()) as mocked_get: result = self.page.get() self.assertEqual(result, 'Hello')
class TestPageRequester(TestCase): def setUp(self): self.page = PageRequester('https://google.com') # Don't want to actually get the contents of google.com # Developer who wrote requests module already wrote tests # Separation between testing our code and requests code # Magic mock - fake values def test_make_request(self): with patch('requests.get') as mocked_get: self.page.get() mocked_get.assert_called() def test_content_returned(self): class FakeResponse: def __init__(self): self.content = 'Hello' with patch('requests.get', return_value=FakeResponse()) as mocked_get: result = self.page.get() self.assertEqual(result, 'Hello')
def setUp(self): self.page = PageRequester("http://google.com")
def setUp(self): self.page = PageRequester('https://google.com')