Run Django Tests

Django’s test client has a different focus. In short: Use Django’s test client to establish that the correct template is being rendered and that the template is passed the correct context data. Use in-browser frameworks like Selenium to test rendered HTML and the behavior of.

You've written some unit tests for your Python app. Good for you! There are dozens of us, dozens!You don't always remember to run your tests, or worse, your colleagues don't always remember to run them.

  • Django’s unit tests use a Python standard library module: unittest.This module defines tests using a class-based approach. Here is an example which subclasses from django.test.TestCase, which is a subclass of unittest.TestCase that runs each test inside a transaction to provide isolation.
  • I had to delete existing Python unittest run configurations for the tests I was trying to run before I could run them as Django tests. I did not need to set any environment variables.

Wouldn't it be nice to automatically run unit tests on every commit to GitHub? What about on every pull request?You can do this with GitHub Actions.You'd be able to hunt down commits that broke the build, and if you're feeling blamey, who broke the build.Sounds complicated, but it's not.Sounds like it might cost money, but the free version has ~30 hours of execution per month.Let me show you how to set this up.

There is example code for this blog post here.

Setting up your project

I'm going to assume that:

  • You have some Python code
  • You use Git, and your code is already in a GitHub repository

If you're already running unit tests locally you can skip this section.Otherwise, your Python project's folder looks something like this:

If you don't have tests already, I recommend trying pytest (and adding it to your requirements.txt).

You'll need at least one test

You'll want to make sure your tests run and pass locally

Set up your Action

You'll need to create new a file in a new folder: .github/workflows/ci.yml.You can learn more about these config files here.Here's an example file:

Run Django Tests For Learning

Now your project looks like this:

Run Django Tests In Docker

Commit your changes, push it up to GitHub and watch your tests run!

Sometimes they fail:

Sometimes they pass:

Add a badge to your README

You can add a 'badge' to your project's README.md.Assuming your project was hosted at https://github.com/MyName/my-project/, you can add thisto your README.md file:

Next steps

Write some tests, run them locally, and then let GitHub run them for you on every commit from now on.If you get stuck, check out this minimal reference or the Actions docs.

November 11, 2017

While PyCharm Pro might be the most popular IDE for Python development, the community version supports neither Django nor JavaScript . VSCode is a free opensource alternative which has pretty good Python support.

Python is supported in VSCode using an extension. A quick start guide on the extension can be found here. It has builtin support for unittest, pytest, and Nose.

You can find information about debugging Django using VSCode here. The extension does not have builtin support for Django UnitTests, but we can use pytest to run Django UnitTests. Please make sure that you have installed pytest and pytest-django. They can be installed using pip.

Shell12[pytest]DJANGO_SETTINGS_MODULE=myproject.settings
Well, That’s all the settings required to run django unit tests. Click the ‘Run Tests’ button on the status bar and select ‘Run All Unit Tests’ from the context menu.

Tests will run and the result summary will be displayed on the output tab. If the test output is not visible, select ‘Run Tests’ and select ‘View Unit Test Output’. Summary of the tests can be viewed from the status bar as well.