测试石墨烯-Django [英] Testing Graphene-Django

查看:43
本文介绍了测试石墨烯-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在研究使用石墨烯构建我的Web服务器API.我已经使用Django-Rest-Framework已有一段时间了,想尝试一些不同的东西.

Currently I am investigating using graphene to build my Web server API. I have been using Django-Rest-Framework for quite a while and want to try something different.

我已经弄清楚了如何将其与现有项目连接起来,并且可以通过键入

I have figured out how to wire it up with my existing project and I can test the query from Graphiql UI, by typing something like

{
   industry(id:10) {
      name
      description
   }
}

现在,我想让单元/集成测试涵盖新的API.问题就从这里开始.

Now, I want to have the new API covered by Unit/integration tests. And here the problem starts.

我要检查的关于石墨烯的查询/执行的所有文档/帖子都在做类似

All the documentation/post I am checking on testing query/execution on graphene is doing something like

result = schema.execute("{industry(id:10){name, description}}")
assertEqual(result, {"data": {"industry": {"name": "Technology", "description": "blab"}}}

我的观点是,execute()中的查询只是一大部分文本,我不知道将来如何维护它.我或将来的其他开发人员必须阅读该文本,弄清楚它的含义,并在需要时进行更新.

My point is that the query inside execute() is just a big chunk of text and I don't know how I can maintain it in the future. I or other developer in the future has to read that text, figure out what it means and update it if needed.

这应该是怎么回事?你们如何编写石墨烯的单元测试?

Is that how this supposed to be? How do you guys write unit test for graphene?

推荐答案

我一直在编写确实有大量查询文本的测试,但是我已经很容易将其粘贴到该大文本中来自GraphiQL.而且我一直在使用RequestFactory来允许我将用户与查询一起发送.

I've been writing tests that do have a big block of text for the query, but I've made it easy to paste in that big block of text from GraphiQL. And I've been using RequestFactory to allow me to send a user along with the query.

from django.test import RequestFactory, TestCase
from graphene.test import Client

def execute_test_client_api_query(api_query, user=None, variable_values=None, **kwargs):
    """
    Returns the results of executing a graphQL query using the graphene test client.  This is a helper method for our tests
    """
    request_factory = RequestFactory()
    context_value = request_factory.get('/api/')  # or use reverse() on your API endpoint
    context_value.user = user
    client = Client(schema)
    executed = client.execute(api_query, context_value=context_value, variable_values=variable_values, **kwargs)
    return executed

class APITest(TestCase):
    def test_accounts_queries(self):
        # This is the test method.
        # Let's assume that there's a user object "my_test_user" that was already setup
        query = '''
{
  user {
    id
    firstName
  }
}
'''
        executed = execute_test_client_api_query(query, my_test_user)
        data = executed.get('data')
        self.assertEqual(data['user']['firstName'], my_test_user.first_name)
        ...more tests etc. etc.

只需从GraphiQL中粘贴"集( {用户{id firstName}} )之间的所有内容,这样便可以根据需要更轻松地进行更新.如果进行更改导致测试失败,则可以将查询从代码粘贴到GraphQL中,并且通常会修复该查询并将新查询粘贴回我的代码中.有目的地在此粘贴的查询上没有制表符,以便于重复粘贴.

Everything between the set of ''' s ( { user { id firstName } } ) is just pasted in from GraphiQL, which makes it easier to update as needed. If I make a change that causes a test to fail, I can paste the query from my code into GraphQL, and will often fix the query and paste a new query back into my code. There is purposefully no tabbing on this pasted-in query, to facilitate this repeated pasting.

这篇关于测试石墨烯-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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