I am facing a problem where i run tests when i just need them as simple methods. for example in test_add_waiter
i must first register a user with self.test_register_user()
. I do this so i wont repeat code (the register_user method works perfectly and i copied it from the actual test class). But the test (test_register_user
) runs as a test and i do not want to test it but use it as a method. What do you propose i do? Is there a way to ignore it in the init of the class. Or am i over thinking this. The are many test classes that i have to face this exact issue.
Example Code
#./tests/test_basics.py
import os
import unittest
from flask import current_app
from app import create_app
from app import db
class BasicsTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app('testing')
self.app_context = self.app.app_context()
self.app_context.push()
self.client = self.app.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
os.remove('../test.db')
self.app_context.pop()
def test_app_exists(self):
self.assertFalse(current_app is None)
def test_app_is_testing(self):
self.assertTrue(current_app.config['TESTING'])
#./tests/application/test_api_app_menu.py
import json
from flask import url_for
from tests.fake_data import create_random_waiter_item
from ..test_basics import BasicsTestCase
class MenuTestCase(BasicsTestCase):
def test_register_user(self):
# http://stackoverflow.com/questions/28836893/how-to-send-requests-with-jsons-in-unit-tests
res = self.client.post(url_for('/auth.authentication'), data=json.dumps({"email": "test@gmail.com",
"username": "test",
"password": "123456"}),
content_type='application/json')
return self.assertTrue(res.status_code == 200)
def test_register_user_id(self):
# http://stackoverflow.com/questions/28836893/how-to-send-requests-with-jsons-in-unit-tests
res = self.client.post(url_for('/auth.authentication'),
data=json.dumps({"email": "test@gmail.com",
"username": "test",
"password": "123456"}),
content_type='application/json')
data = json.loads(res.data)
self.assertTrue(data["user_id"] == 1)
def test_add_waiter(self):
# register user in order to register a store.
self.test_register_user()
sample_item = {"items": create_random_waiter_item()[0]}
res = self.client.post(url_for('/manage.waiter')+"?store_id=1", data=json.dumps(sample_item),
content_type='application/json')
# data = json.loads(res.data)
self.assertTrue(res.status_code == 201)
# Todo 19/3/2017 - fix the unique constraint error wher registered user is ran along with register user
def test_get_menu(self):
# register user in order to register a store.
# self.test_register_user()
self.test_add_waiter()
res = self.client.get(url_for('/app.menu')+"?store_id=1&cell_phone=1234567890&pin_number=1234",
content_type='application/json')
self.assertTrue(res.status_code == 200)
def test_get_menu_bad_url(self):
# register user in order to register a store.
self.test_add_waiter()
res = self.client.get(url_for('/app.menu')+"?store_id=&cell_phone=6900000000&pin_number=123456",
content_type='application/json')
self.assertTrue(res.status_code == 400)