As I develop the backend side, I also develop the frontend. As you have probably guessed, I have to do some REST requests on my server.
Well, there are a couple of solutions for that. I can use mocks on the frontend, or a fake JSON server. However, there are some cons to this approach:
- I have to remodel what I have on an already existing backend server.
- Dealing with authentication on a fake JSON server is not really comfortable.
- I have to write a lot of boilerplate if I want to go on mocking and unit testing approach.
Again, all the logic I want already exists in my Django project, so what I want is:
- Run a dev server.
- Set up a fresh new database.
- Provide some initial data. Some fake users, posts, whatever.
- Do my testing on my frontend project while the server runs.
- Hit the kill signal. (CTRL C)
- Gracefully drop the database that I have created on step 2.
- Shutdown the program.
I use pytest
and pytest-django
. They do exactly these when they set up a test environment, I was wondering if I can manually do that.
Thanks in advance.
Environment
- Python 3.9
- Django 2.2
CodePudding user response:
I never used such integration-testing setup, but I’ve seen it somewhere.
You can run the django-admin testserver mydata.json
command to spin up a testing server with data from a provided fixture. The way it was done in the example I saw was by making a custom Django command where instead of static fixture the testserver’s database was populated dynamically by factories. I don’t remember the details how it was implemented, though.
After the testserver was ready, the Cypress tests were run. Backend and the frontend were wired by npm’s start-server-and-test
package.
Application with similar setup can be checked out here. Especially note the custom testserver command as well as the cypress commands.