Run Django Tests

Default shortcuts: Run Closest Test Method: cmd+d+m extension.djangoTestRunner.runMethodTests Run Closest Test Class: cmd+d+c extension.djangoTestRunner.runClassTests Run Current Test File: cmd+d+f extension.djangoTestRunner.runFileTests Run Current App Tests: cmd+d+a extension.djangoTestRunner.runAppTests Run Previous Tests: cmd+d+p extension.djangoTestRunner.

  • To run only migrations tests, use -tag option: python mange.py test-tag = migrationtest # runs only migraion tests python mange.py test-exclude-tag = migrationtest # runs all except migraion tests Django Checks. Djangotestmigrations comes with 2 groups of Django's checks for: detecting migrations scripts automatically generated names.
  • 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.
  • Configuring django project ¶. To enable djangojenkins you need only to add djangojenkins to INSTALLEDAPPS in settings.py. $ python manage.py jenkins. Will do the same job as. $ python manage.py tests. But also will create reports folder in the root of your django project with jenkins parsable pylint, test coverage and tests reports.
  • 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.

The beauty of Azure DevOps is it support to many technologies and all of major language.s I have a simple git repository where I’m experimenting Python code, in that repository I have several directories like 020_xxxx 010_yyy where I’m playing with Python code.

Django Unit Testing

Each folder contains some code and some unit tests written in Pytest, my goal is creating an Azure Pipeline that can automatically run all pytest for me automatically each time I push some code to the repository. > Even if Phyton is a script languages, it has several Unit Testing frameworks that can be used to verify that the code you wrote works as expected

Creating a build in Azure DevOps is really simple, just create a build that points to a yaml file in your repository that contains the definition.

This is a real simple yaml buid definition where I’m simply requiring the usage of python 3.x, then install some packages with pip and finally a series of pytest tests run for each folder. As you can see I specified also the trigger to automatically build all branches typical of GitFlow naming convention.

The trick to have the result of the tests published directly in your build result is using a Pytest option to create a result file with JUNIT xml file format ; once you have test result as a JUNIT xml files you can use standard PublishTestResults task to publish results in the build.

After the build completed you can simply looks at the output, if everything is ok the build is Green.

Figure 1: Test results in my build that shows results of my python unit tests.

The build will run all python tests in all of my source code folder, thanks to Pytest that does everything for you, both discovering and run all tests in the folder.

If code does not compile, unit test will fail and you have a failing build.

Figure 2: Not compiling code will lead to a failing build.

The problem with this approach is that the build stops at the very first error, so if an error happens in 010 directory my 020 directory will not be tested because at the very first failed test the execution of the build stopped.

Django

This condition is usually too strict, for unit testing it is usually a better approach to configure the build to continue even if run test failed.**To accomplish this, just add continueOnError: trueafter each bash task used to run tests. **With continueOnError equal to true, the build will continue and if some of the test task fails, the build is marked as Partially Failed, and from the summary you can easily check the run that generates the error.

Django

** Figure 3: ***Continue on error true will make the build continue on test error, in the summary you can verify what failed,*

The reason why I choose to launch a different Pytest run for each folder and upload each result with a different task is to have separate run in build result.

Figure 4: Test runs are distinct for each folder.

Even if this will force me to add two task for each folder (one for the run and the other for the publish) this approach will give me a different result for each folder so I can immediately understand where is the error.

Run django server

Run Django Server

Django

Django Test Client

Gian Maria.