1

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

FallenSpaces
  • 312
  • 1
  • 4
  • 17
  • What exactly is the text it shows? I also notice that you didn't `import pytest` in `test_main.py`, and you've got your assert unindented (it should be in the test function). That leads me to believe that you're using the standard Python assert, and not the one provided by pytest. – Andrea Reina Jun 01 '16 at 01:18
  • I've edited to show output. It shows that pytest is running, and I'm following how it shows in the "Getting started" [here](http://pytest.org/latest/getting-started.html#getstarted). – FallenSpaces Jun 01 '16 at 01:42
  • Use `response.status_code == 200` – Padraic Cunningham Jun 01 '16 at 09:31

2 Answers2

1
### 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.

Dmitry Tokarev
  • 1,851
  • 15
  • 29
0

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.

Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
  • This is more of a way to get my feet wet with testing. I was only using this because it seemed like the easiest thing to test. So lets just say this is a unique part of my program, not a response test. Would I be going about this the right way? – FallenSpaces Jun 01 '16 at 00:27
  • @Manix I think, it's sort of hard to say for this (slightly contrived) example... because the function is essentially one line that calls `requests.get(...)`. You might try storing the response in a `response` object as @padraic-cunningham mentioned above. In any case, you don't really want your unit test to be performing network requests (too slow, mixed separation of concerns). You might also find this answer provides helpful additional context on a related question. http://stackoverflow.com/a/11399210/149428 – Taylor D. Edmiston Jun 02 '16 at 00:18