我如何测试我的Flask dev服务器是否启动? [英] How do I test if my Flask dev server is up?

查看:109
本文介绍了我如何测试我的Flask dev服务器是否启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Flask应用程序上有一个测试装置,启动开发服务器来测试一些用户交互。对于第一次测试,我只想确保服务器已经启动。一般这样做的最好方法是什么(没有测试特定的响应代码)?我希望我可以使用 self.assertTrue(response),但即使我改变端口,似乎也会通过。



  class TestAuth(TestCase):

def create_app (self):
db.init_app(app)
return app

@classmethod
def setUpClass(cls):
cls.server_proc = subprocess。 Popen(
[sys.executable,'run.py'],
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT)
time.sleep(5)

@classmethod
def TearDownLcass(cls):
os.kill(cls.server_proc.pid,signal.SIGINT)
$ b $ def setUp(self):
selenium_logger = logging.getLogger(
'selenium.webdriver.remote.remote_connection')
selenium_logger.setLevel(logging.WARN)
self.browser = webdriver.Firefox()

db.create_all()

de f tearDown(self):
db.session.remove()
db.drop_all()
self.browser.quit()
$ b $ def test_server_up(self) :
response = self.client.get('http:// localhost:5000 /')
self.assertTrue(response)

测试由 Flask-Testing 公开的样式不需要单独的服务器启动。 self.client.get 调用并不真正从服务器请求URL,它只是将请求内部路由到运行测试的相同进程中。



如果要使用单独的服务器,则不能使用 self.client 来请求URL,在这种情况下,您必须使用正确的HTTP客户端。看来你已经实例化一个Selenium浏览器,所以这将工作得很好。将 self.client.get()替换为 self.browser.get()



另一方面,如果您并不是特别想要启动一个单独的Web服务器,并且可以使用Flask和Flask-Testing提供的默认工具来测试您的应用程序,那么将所有服务器进程启动代码和Selenium的东西,只需使用 self.client 发出请求。但请注意, self.client 不是一个真正的Web客户端,所以测试服务器是否启动没有意义,因为在这种测试中没有服务器。

I have a test fixture on my Flask app which launches the dev server to test some user interactions. For the first test, I'd like to just insure the server is up. What's the best way of doing this in a generically (without testing for a specific response code)? I was hoping I could use self.assertTrue(response), but that seems to pass even if I change the port.

Here's the test case in question:

class TestAuth(TestCase):

    def create_app(self):
        db.init_app(app)
        return app

    @classmethod
    def setUpClass(cls):
        cls.server_proc = subprocess.Popen(
            [sys.executable, 'run.py'],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)
        time.sleep(5)

    @classmethod
    def TearDownLcass(cls):
        os.kill(cls.server_proc.pid, signal.SIGINT)

    def setUp(self):
        selenium_logger = logging.getLogger(
            'selenium.webdriver.remote.remote_connection')
        selenium_logger.setLevel(logging.WARN)
        self.browser = webdriver.Firefox()

        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.browser.quit()

    def test_server_up(self):
        response = self.client.get('http://localhost:5000/')
        self.assertTrue(response)

解决方案

You are mixing up two different ways to test.

The testing style exposed by Flask-Testing does not require a separate server to be started. The self.client.get call does not really request a URL from a server, it just routes requests internally into the same process that is running the test.

If you want to work with a separate server, then you cannot use self.client to request URLs, in that case you have to use a proper HTTP client. It seems you already instantiated a Selenium browser, so that would work just fine. Replace self.client.get() with self.browser.get().

If, on the other side, you don't specifically want to start a separate web server and are okay testing your app using the default facilities provided by Flask and Flask-Testing, then remove all the server process start up code and the Selenium stuff and just use self.client to make requests. But be aware that self.client is not a real web client, so there is no point in testing if the server is up, because there is no server when testing this way.

这篇关于我如何测试我的Flask dev服务器是否启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆