FallenSpaces
FallenSpaces

Reputation: 352

Correctly test with pytest

So I want to start using tests with pytest in my python programs.

EDIT: I'm only trying to test the response because it seemed like the easiest thing to test. I now understand that there are multiple ways to test the response, but I'm more looking to just get a general grip on building tests and using them.

I'm starting by testing if the correct response happens when I call a page using requests.

Like so:

**main.py**

def get_page(search_url):
  page = requests.get(search_url)
  return page

url = "https://www.google.com/search?q=weather+results&oq=weather+results&aqs=chrome..69i57.4626j0j1&sourceid=chrome&ie=UTF-8"

get_page(url)

Here is the test code I made to test the response. This is the first test I've ever written.

**test_main.py**

from main import get_page

def test_page_response():

   test_url = "https://www.google.com/search?q=weather+results&oq=weather+results&aqs=chrome..69i57.4626j0j1&sourceid=chrome&ie=UTF-8"

   assert str(get_page(test_url2)) == "<Response [200]>"

Am I doing this right? When I take out the url to break it and trigger a test, it shows me a ton of text. Sure, it's the error in it's full glory, but isn't testing supposed to make this simpler to read and understand what broke?

This leads me to believe I'm going about this the wrong way.

EDIT 2: Here's the output: http://pastebin.com/kTgc5bsR

Upvotes: 1

Views: 7782

Answers (2)

Dmitry Tokarev
Dmitry Tokarev

Reputation: 2089

### test_main.py ###

from main import get_page

def test_page_response():
   test_url = "https://www.google.com/search?q=weather+results&oq=weather+results&aqs=chrome..69i57.4626j0j1&sourceid=chrome&ie=UTF-8"

    response = get_page(test_url2)  # get_page() returns a response object
    assert response.status_code == 200

    # also here reponse.text will contain the html string.
    # You can parse it and have more assertions.
    # This will be your real test to see if you got search results you expected.

Read more on how to use python-requests:

http://docs.python-requests.org/en/master/

Your url is basically your test input, you may modify the url to generate tests. I suggest going through py.test basic examples:

http://pytest.org/latest/example/index.html

and also taking a primer on testing in general.

Upvotes: 2

Taylor D. Edmiston
Taylor D. Edmiston

Reputation: 13016

Is your goal to write a unit test?

If so, testing requests.get is already covered by rhe tests inside requests. It's considered unpythonic (and redundant) to re-check something that Python or your library already test for you. Instead, you should focus on testing the unique part of your app.

For example, mock the usage of requests. One way to do that is with the library requests-mock, though of course there are more one off approaches too.

Assuming you've mocked requests, the way I'd approach writing a unit test for get_page(...) is to assert that it returns the expected response body. You could also test for status code, but if you're mocking the request, this may not add a ton of value.

You may also consider testing retrieving the webpage itself in an integration test.

I'm happy to add code examples here if it would make it clearer.

Upvotes: 0

Related Questions