lundi 4 avril 2016

delete inserted data after doing unit testing

I did tried to do the unit testing in flask. And I did, I'm testing a function that saves documents in database. I want to after finishing those testing function to apply tearDown function to delete all the data that were added in database while testing.

I'm using flask and mongo db. In this documentation Here I saw that they use flaskr, but I couldnt import it.

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

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

I tried to do it like below, but actually I'm not sure how to do that. Is better to use a database only for testing or should I post in main database? Should I import flaskr?

from app import create_app, mongo
...
class ApiTestCases(TestCase):

    def create_app(self):
        app = create_app()
        return app

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_saving_dummy_json(self):

        json_obj =  {"test": "test", "post_type": "test"}
        response = self.client.post(
            url_for('api.save'),
            data=json.dumps(json_obj),
            content_type='application/json'
        )
        self.assert200(response)

Please help!

Aucun commentaire:

Enregistrer un commentaire