Start and Take down bank in flask test

Asked

Viewed 288 times

5

I’m doing an api test, which I pass a json it validates me if it came back all ok:

my base class:

# -*- coding: utf-8 -*-
# base.py

import os
import unittest
from app import initialize
from mock import mock
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from app import database
os.environ = {
    my_variavels:
}

test_app = Flask(__name__)
test_app.config.from_object('app.config.TestingConfig')
database.AppRepository.db = SQLAlchemy(test_app)

class TestCase(unittest.TestCase):
    mock = mock
    user = initialize.web_app.test_client()

I believe that in this base file, should have the functions teardown, Setup etc..

 json_gardener = {"id": "1",
                    "name": "Jamme"}

And I have the following function that makes the post in this case:

def test_post_gardener(self):
             response = self.user.post('/gardeners', headers={'VALIDATION': 'XXXX'}, data=self.json_gardener)
            self.assertEqual(response.status_code, 201)

So far everything ok, works perfectly, but when I run the second time obviously it will already exist the id in the bank and will give error.

My doubt the following, there is a way to upload a database to test and when finished running it disassemble?

  • 1

    How’s your test structure? I suggest you use unittest with a database like sqlite, and in unittest there is the tearDown() class method that runs at the end of the run, so you could have a routine to clean the bank. Could you give more details?

  • I’m using the nosetest, bench postgres can tell me more of the teardown method, and how I start the bank?

  • If the bank is testing, you can’t just send one DELETE for API and erase the record? Still testing the method DELETE application. Unless you use it to disable the guy, then it’s really better to have a test cleaning function that only gives TRUNCATE in the same tables.

  • Using classes or only isolated methods?

  • class, after lunch I will detail better the question

  • Solved? The problem..

  • Negative, I added more information to the question

  • Is it part of the rule to generate the id in the application layer and not with a sequential one in the database? Or you’re simply doing this in the Test Method Feed to be able to test the ID already having the value previously?

Show 3 more comments

1 answer

1


According to Flask’s own documentation there is the class tempfile which can be used for this as a database instance in the test class.

import os
import flaskr
import unittest
import tempfile

class FlaskrTestCase(unittest.TestCase):

    def setUp(self):
        self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
        flaskr.app.config['TESTING'] = True
        self.app = flaskr.app.test_client()
        with flaskr.app.app_context():
            flaskr.init_db()

    def tearDown(self):
        os.close(self.db_fd)
        os.unlink(flaskr.app.config['DATABASE'])

    def test_empty_db(self):
        rv = self.app.get('/')
        assert b'No entries here so far' in rv.data

    def login(self, username, password):
    return self.app.post('/login', data=dict(
        username=username,
        password=password
    ), follow_redirects=True)

    def logout(self):
        return self.app.get('/logout', follow_redirects=True)

    def test_login_logout(self):
        rv = self.login('admin', 'default')
        assert b'You were logged in' in rv.data
        rv = self.logout()
        assert b'You were logged out' in rv.data
        rv = self.login('adminx', 'default')
        assert b'Invalid username' in rv.data
        rv = self.login('admin', 'defaultx')
        assert b'Invalid password' in rv.data

if __name__ == '__main__':
    unittest.main()

Source: http://flask.pocoo.org/docs/0.12/testing/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.