Pytest for ex51

Finally I have achieved my goal. To write all the test with Pytest instead of Nose.

This is the equivalent test.

import pytest

from ex50.app import app
app.config['TESTING'] = True
client = app.test_client()

web = app.test_client()

@pytest.fixture()
""" This fixture is for the two test that have a GET parameter and '/hello'.
The other test is just one of each type. No use to give them fixtures """
def web_get():
    rv = web.get('/hello', follow_redirects=True)
    return rv

def test_404():
    rv = web.get('/', follow_redirects = True)
    assert rv.status_code == 404

def test_200(web_get):
    assert web_get.status_code == 200

def test_fill_form(web_get):
    assert b'Fill Out this form please.' in web_get.data  

def test_data():
    data = {'name': 'Ulf', 'greet': 'Hello'}
    rv = web.post('/hello', follow_redirects=True, data=data )
    assert b'Ulf' in rv.data
    assert b'Hello' in rv.data

Now off to the study drills before the last exercise.

1 Like

Great, man!
I’m always confused with that fixtures thing. Have to look more into that.

Hello @DidierCH

It wasn’t easy to get this into my head.
There is a gap between beginners level and advanced level. If the example not are over easy they are quite complicated (my opinion based on my level).
It was hard to find a example with some basic function.

I also learned that this fixture and other can be put in a conftest.py file in the same level as the tests/ directory.
Then it can be used in any test_*.py files in the tests/ directory that need this fixture.
Pretty convinient. :+1:

1 Like

Do you have that example anymore? Would be interested in seeing it, because it’s like you said: the examples are either over simple or to complicated)

Hello @DidierCH.

I am reading this book (Python testing with Pytest) at the moment to learn how to do the test parts in Zeds book.

One thing came to my mind.
Perhaps I try to fill in the gap between basic and advanced. In here as a thread.
What do you think?

Hey @ulfen69 that would be fantastic.

Hmm, that book is on my list as one of the next to read. I’m currently learning Django but testing is next.

I will just write down what I have learned so far.
Pieces that I had to work a little bit to understand. Not a complete school.
Just a little bridge over the “gap”
Except for helping others (hopefully), this also help myself.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.