python - 针对远程服务器运行 Flask 测试

标签 python testing flask

我正在为我的 flask api 编写“单元测试”。在本地使用 Flask.test_client() 很容易。例如:

app = Flask(__name__)
client = app.test_client()

但是,我仍然希望能够做一些事情,比如

self.client.post('{0}/account/password'.format(self.base_path),
                              data=json.dumps(passwordu), headers=self.get_headers(),
                              content_type=self.content_type)

但我希望客户端与远程主机对话。

我看到我可以使用 environ_override 作为 client.post(...) 方法的参数。对我来说,必须有一种更简单的方法来获得一个简单的客户端,该客户端以与 test_client() 相同的方式执行必要的请求发送和响应解码。但是,子类化客户端仍然需要我使用 Flask 实例。

最佳答案

好的,首先,感谢 Anarci 的建议。这是我想出的。

我不认为它是好的 OO 设计,但它确实有效而且很懒惰。

class RemoteApiTest(AppTest, ApiTestMixin):
    @classmethod
    def setUpClass(cls):
        super(RemoteApiTest, cls).setUpClass()
        cls.kwargs = config.FLASK_CLIENT_CONFIG
        cls.base_url = cls.kwargs.get('base_url')
        cls.content_type = cls.kwargs.get('content_type')
        cls.client = requests
        cls.db = db
        cls.db.create_all()

    @classmethod
    def tearDownClass(cls):
        super(RemoteApiTest, cls).tearDownClass()
        cls.db.session.remove()
        cls.db.drop_all()
        #cls.db.get_engine(cls.app).dispose()

    @property
    def base_url(self):
        return self.__class__.base_url

    @property
    def content_type(self):
        return self.__class__.content_type

    def get_json(self, r):
        return r.json()

    @property
    def headers(self):
        headers = {'content-type': self.content_type}
        if self.api_key:
            headers.update({'X-consumer-apiKey': self.api_key})
        return headers

    def basic_checks(self, rv):
        eq_(rv.status_code, 200)
        r = rv.json()
        assert rv is not None
        assert r is not None
        assert r.get('correlationID') is not None
        return r

    def setUp(self):
        super(RemoteApiTest, self).setUp()
        self.api_key = None

        r = self.client.post('{0}/account/register'.format(self.base_url), data=json.dumps(account_register),
                             headers=self.headers)
        j = r.json()
        self.api_key = j['apiKey']

    def tearDown(self):
        rv = self.client.delete('{0}/account'.format(self.base_url), headers=self.headers)
        super(RemoteApiTest, self).tearDown()

有了它,我可以在我的相同配置中配置远程服务器等,我可以重用这样的代码:

class ApiTestMixin:
    def test_account_get(self):
        rv = self.client.get(self.base_url + '/account', headers=self.headers)
        r = self.basic_checks(rv)
        account = r.get('account')
        eq_(account.get('fullName'), account_register.get('fullName'))
        assert r.get('correlationID') is not None

绝对愿意接受批评,但这就是我让它发挥作用的方式,

关于python - 针对远程服务器运行 Flask 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32512699/

相关文章:

python - Google App Engine 上的批量加载程序 : Incorrect credentials or unsupported authentication type (e. g。打开ID)

java - 方法取决于不存在的组 - Testng

python - 上下文中的 flask 上下文,对于 jsonfy,全部在一个页面中

python - 服务器重启后如何使Flask应用启动并运行?

python - 前向链表的问题

python - 让python脚本在特定时间内每天运行

testing - 错误元素当前不可见,因此可能无法与 Selenium 交互

ruby-on-rails - 如何测试 ActiveAdmin?

python - 在 flask 中使用 SqlAlchemy 模型

Python散点图在背景图像上进行数据验证