0

I am having a problem with making automated tests for flask in python 3. I have tried unittest, pytests, nosetests but I still can't figure out how to form automated tests for flask application.

Following is the code I have wrote using unittest and pytest
unittest:

import unittest
from flaskfolder import flaskapp

class FlaskBookshelfTests(unittest.TestCase): 

    @classmethod
    def setUpClass(cls):
            pass 

    @classmethod
    def tearDownClass(cls):
            pass 

    def setUp(self):
            # creates a test client
            self.flaskapp = flaskapp.test_client()
            # propagate the exceptions to the test client
            self.flaskapp.testing = True

    def tearDown(self):
            pass

    def test1(self):
            result = self.flaskapp.get('/')

            self.assertEqual(result.status_code, 200) 

In this code i am having error that flaskapp doesn't has any test_client() function.
pytest:

import pytest
from flaskfolder import flaskapp
@pytest.fixture
def client():
    db_fd, flaskapp.app.config['DATABASE'] = tempfile.mkstemp()
    flaskapp.app.config['TESTING'] = True

    with flaskapp.app.test_client() as client:
            with flaskapp.app.app_context():
                    flaskapp.init_db()
            yield client

    os.close(db_fd)
    os.unlink(flaskapp.app.config['DATABASE'])
def test1():
    result = client.get('/')

    assert result.data in 'Hello World'

In this error that "'function' doesn't has any attribute get" is recieved and if done: def test1(client) it gives an error that flaskapp doesn't have any attribute init_db.

Icy
  • 13
  • 7

1 Answers1

0

Because client is a pytest fixture, you need to include it as anargument to your test, so this should solve your current issue

def test1(client):
    result = client.get('/')

    assert result.data in 'Hello World'
ilmarinen
  • 4,557
  • 3
  • 16
  • 12
  • if done that it gives the error that flaskapp doesn't have any attribute init_db, I have also added it to the question – Icy Jun 04 '20 at 17:33