From 44a8526807a78554f8aaa38160948890064ed827 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 28 Dec 2016 13:38:55 +0000 Subject: [PATCH] use `pytest.mark.usefixtures` instead of funcargs when you invoke the fixture `sample_user`, it does two things: it creates the user in the database, but also returns the user, a useful object that you may want to manipulate or reference in your test. however, when you invoke the fixture `notify_db_session`, it doesn't do anything - rather, it *promises* to clear up the database tables at the end of the test run. because we have no need of the notify_db_session object in our tests (indeed, for a long time this fixture just returned `None`), using `pytest.mark.usefixtures('notify_db_session')` brings attention to the fact that this is a side-effect fixture rather than a data setup fixture. Functionally it is identical to passing as a parameter --- tests/app/dao/test_users_dao.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/app/dao/test_users_dao.py b/tests/app/dao/test_users_dao.py index 75ddd279f..0bc417b21 100644 --- a/tests/app/dao/test_users_dao.py +++ b/tests/app/dao/test_users_dao.py @@ -1,10 +1,10 @@ from datetime import datetime, timedelta + from sqlalchemy.exc import DataError from sqlalchemy.orm.exc import NoResultFound - -from app import db import pytest +from app import db from app.dao.users_dao import ( save_model_user, save_user_attribute, @@ -21,7 +21,8 @@ from app.models import User, VerifyCode from tests.app.db import create_user -def test_create_user(notify_db_session): +@pytest.mark.usefixtures('notify_db_session') +def test_create_user(): email = 'notify@digital.cabinet-office.gov.uk' data = { 'name': 'Test User', @@ -37,7 +38,8 @@ def test_create_user(notify_db_session): assert not user.platform_admin -def test_get_all_users(notify_db_session): +@pytest.mark.usefixtures('notify_db_session') +def test_get_all_users(): create_user(email='1@test.com') create_user(email='2@test.com') @@ -45,18 +47,21 @@ def test_get_all_users(notify_db_session): assert len(get_user_by_id()) == 2 -def test_get_user(notify_db_session): +@pytest.mark.usefixtures('notify_db_session') +def test_get_user(): email = '1@test.com' user = create_user(email=email) assert get_user_by_id(user_id=user.id).email_address == email -def test_get_user_not_exists(notify_db_session, fake_uuid): +@pytest.mark.usefixtures('notify_db_session') +def test_get_user_not_exists(fake_uuid): with pytest.raises(NoResultFound): get_user_by_id(user_id=fake_uuid) -def test_get_user_invalid_id(notify_db_session): +@pytest.mark.usefixtures('notify_db_session') +def test_get_user_invalid_id(): with pytest.raises(DataError): get_user_by_id(user_id="blah")