jarus / flask-testing

Unittest extensions for Flask
http://pythonhosted.org/Flask-Testing/
Other
502 stars 111 forks source link

Documented examples don't run #121

Closed fenimore closed 5 years ago

fenimore commented 6 years ago

Hi, I'm using Python 2 and

Flask==0.12.2
Flask-Testing==0.7.1

Hi, I'm trying to use flask-testing, and so I've tried running the documented quickstart way, and also I've copied some of the tests and tried running them in my project and I've had no avail. So for example, if I run this:

class BaseTestLiveServer(LiveServerTestCase):

    def test_server_process_is_spawned(self):
        process = self._process

        # Check the process is spawned
        self.assertNotEqual(process, None)

        # Check the process is alive
        self.assertTrue(process.is_alive())

    def test_server_listening(self):
        response = urlopen(self.get_server_url())
        self.assertTrue(b'OK' in response.read())
        self.assertEqual(response.code, 200)

class TestLiveServer(BaseTestLiveServer):

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True
        # Default port is 5000
        app.config['LIVESERVER_PORT'] = 8943
        # Default timeout is 5 seconds
        app.config['LIVESERVER_TIMEOUT'] = 10
        return app

I get:

exceptions.AttributeError: 'TestLiveServer' object has no attribute '_process'

and if I run (directly from the docs):


class MyTest(LiveServerTestCase):

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True
        # Default port is 5000
        app.config['LIVESERVER_PORT'] = 8943
        # Default timeout is 5 seconds
        app.config['LIVESERVER_TIMEOUT'] = 10
        return app

    def test_server_is_up_and_running(self):
        response = urllib2.urlopen(self.get_server_url())
        self.assertEqual(response.code, 200)

I get:

exceptions.AttributeError: 'MyTest' object has no attribute '_port_value'

or if I run:

class MyTest(TestCase):

    def create_app(self):

        app = Flask(__name__)
        app.config['TESTING'] = True
        return app

    def test_server_is_up_and_running(self):
        response = urllib2.urlopen(self.get_server_url())
        self.assertEqual(response.code, 200)

I get:

exceptions.AttributeError: 'MyTest' object has no attribute 'get_server_url'
edward-lim commented 6 years ago

@fenimore Try adding the main() method to the end of your tests as documented here:

http://flask-testing.readthedocs.io/en/latest/#running-tests

edward-lim commented 6 years ago

Actually, it looks like you don't even need that. You just need to run it with either pytest or nose.

yufuluo commented 6 years ago

Is this problem solved? After adding the main() method to the end of my test file, I still got the same issue 'MyTest' object has no attribute 'get_server_url'

darkn3rd commented 5 years ago

Noticed the documentation is rather, vacant. Any good references to full app, such as hello world flask app and a script with test cases? I cannot get off the ground with current documentation provided.