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.
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.
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.
This didn’t work for me when I typed in the code. However, I noticed my code did not work unless I put gothonweb.app when importing app. Not sure why this is since normally for pytest just indicating the module has worked for me.
This is what I wrote for ex51 with pytest:
Without the gothonweb.app I always get this error in Powershell:
AttributeError: module ‘gothonweb.app’ has no attribute ‘config’
Hope this helps anyone trying to use pytest instead of Nose