简体   繁体   中英

Django WebServer During Testing

I'm writing a complicated web application in Django. 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 . When Server wants a calculation done, it sends a command to a socket on which Calculator is listening. Like this:

{
    "command": "doCalculations"
}

Now, Calculator might need different pieces of information at different times to do its work. So instead of passing the data directly to Calaculator in the command, it is up to Calculator to ask for what it needs. It does this by calling a RESTful API on Server :

https://Server/getStuff?with=arguments

Calculator then uses the data from this call to do its calculations, and respond to Server with an answer.

The problems begin when I try to do unit testing using Djangos unittest framework. 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. 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.

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

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 .

It will be filled with data from your main db in JSON format.

data.json should exist in the fixtures folder of the app you are testing. (Create one if necessary).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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