简体   繁体   English

测试期间的Django WebServer

[英]Django WebServer During Testing

I'm writing a complicated web application in Django. 我正在用Django编写一个复杂的Web应用程序。 There are many components. 有很多组件。 Two in particular, are the Django server (lets call this Server ), and a C++ application server (lets call this Calculator ) which serves calculations to Server . 特别是两个,分别是Django服务器(以下称此Server )和一个C ++应用程序服务器(以下称此Calculator ),该ServerServer提供计算。 When Server wants a calculation done, it sends a command to a socket on which Calculator is listening. Server希望完成计算时,它将命令发送到正在监听Calculator的套接字。 Like this: 像这样:

{
    "command": "doCalculations"
}

Now, Calculator might need different pieces of information at different times to do its work. 现在, Calculator可能需要在不同时间提供不同的信息来完成其工作。 So instead of passing the data directly to Calaculator in the command, it is up to Calculator to ask for what it needs. 因此,不是将数据直接传递给命令中的Calaculator ,而是由Calculator询问其需要什么。 It does this by calling a RESTful API on Server : 它通过在Server上调用RESTful API来实现:

https://Server/getStuff?with=arguments

Calculator then uses the data from this call to do its calculations, and respond to Server with an answer. 然后, Calculator将使用此调用中的数据进行计算,并使用答案答复Server

The problems begin when I try to do unit testing using Djangos unittest framework. 当我尝试使用Django的unittest框架进行单元测试时,问题就开始了。 I set up a bunch of data structures in my test, but when Server calls Calculator , it needs to have this data available in the REST API so Calculator can get what it needs. 我在测试中设置了一堆数据结构,但是当Server调用Calculator ,它需要在REST API中提供此数据,以便Calculator可以获取所需的数据。 The trouble is that the Django test framework doesn't spin up a webserver, and if I do this manually it reads the data from the real database, and not the test-case. 问题在于Django测试框架不会启动Web服务器,如果我手动执行此操作,它将从真实数据库而不是测试用例中读取数据。

Does anybody know how to run a unit test with the data made available to external people/processes? 有谁知道如何使用外部人员/流程可用的数据运行单元测试?

I hope that makes sense... 我希望这是有道理的...

You need specify the fixtures to load in your test class. 您需要指定要加载到测试类中的灯具。

https://docs.djangoproject.com/en/1.7/topics/testing/tools/#fixture-loading https://docs.djangoproject.com/en/1.7/topics/testing/tools/#fixture-loading

class MyTest(TestCase):
    fixtures = ['data.json']

    def setUp(self):
        # do stuff

    def tearDown(self):
        # do stuff

Where data.json can be retrieved by using python manage.py dumpdata . 可以使用python manage.py dumpdata检索data.json python manage.py dumpdata

It will be filled with data from your main db in JSON format. 它将以JSON格式填充来自主数据库的数据。

data.json should exist in the fixtures folder of the app you are testing. data.json应该存在于您正在测试的应用程序的fixtures文件夹中。 (Create one if necessary). (如有必要,创建一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM