<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Asdrubal Santander</title>
    <description>The latest articles on DEV Community by Asdrubal Santander (@asdrubalsantander).</description>
    <link>https://dev.to/asdrubalsantander</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F185953%2F68379eb5-8a0b-4623-aa7f-7e9ececfa0ad.jpeg</url>
      <title>DEV Community: Asdrubal Santander</title>
      <link>https://dev.to/asdrubalsantander</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asdrubalsantander"/>
    <language>en</language>
    <item>
      <title>Gitlab CI/CD Multi-Project Pipeline</title>
      <dc:creator>Asdrubal Santander</dc:creator>
      <pubDate>Sun, 26 Jul 2020 20:53:46 +0000</pubDate>
      <link>https://dev.to/asdrubalsantander/gitlab-ci-cd-multi-project-pipeline-231l</link>
      <guid>https://dev.to/asdrubalsantander/gitlab-ci-cd-multi-project-pipeline-231l</guid>
      <description>&lt;h1&gt;
  
  
  Gitlab CI/CD
&lt;/h1&gt;

&lt;p&gt;Gitlab CI/CD is the continuous integration and continuous delivery solution created by Gitlab, if you need help understanding the basics please refer to other sources like: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://about.gitlab.com/stages-devops-lifecycle/continuous-integration/" rel="noopener noreferrer"&gt;Gitlab&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=sIegJaLy2ug" rel="noopener noreferrer"&gt;Getting started with GitLab CI/CD - GitLab Unfiltered&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=Jav4vbUrqII" rel="noopener noreferrer"&gt;Gitlab CI pipeline tutorial for beginners - Valentin Despa&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Multi-Project Pipeline
&lt;/h1&gt;

&lt;p&gt;Is a feature that allows us to trigger pipelines that are hosted in other projects.&lt;/p&gt;

&lt;p&gt;This is especially useful for microservices projects that need a way to deploy everything in the correct order, for projects that need to run E2E tests before deploying to production, or any other case that you can think of that needs integrated pipelines between projects.&lt;/p&gt;

&lt;p&gt;Launching another's project pipeline was initially introduced to Gitlab in the form of API &lt;a href="https://docs.gitlab.com/ee/ci/triggers/README.html" rel="noopener noreferrer"&gt;Triggers&lt;/a&gt;. Triggers are excellent when you need to launch a pipeline outside of Gitlab or for another project that you or your organization do not own. But the use of this type of triggers needs additional configurations like create a trigger token or use curl inside the job script.&lt;/p&gt;

&lt;p&gt;Multi-Project Pipeline became free for all users in early 2020, but without the visualization of the pipeline triggered, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.gitlab.com%2Fee%2Fci%2Fimg%2Fmulti_project_pipeline_graph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.gitlab.com%2Fee%2Fci%2Fimg%2Fmulti_project_pipeline_graph.png" alt="gitlab-multi-project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, we are going to explain some of the configurations in the Multi-Project Pipeline feature and from a free account ;)&lt;/p&gt;

&lt;h1&gt;
  
  
  Use Case:
&lt;/h1&gt;

&lt;p&gt;Let's assume the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are developing an awesome app by our development team.&lt;/li&gt;
&lt;li&gt;The development team has already done some functionality of the awesome app and is already deployed to production.&lt;/li&gt;
&lt;li&gt;But we also have a QA team that needs to make E2E testing to our awesome app.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Projects
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;my-awesome-project: This is the project with the code we want to deploy to production.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;e2e-testing-qa: This is the project we want to trigger before the deployment phase in &lt;code&gt;my-awesome-project&lt;/code&gt;, the main purpose of this project is to do E2E testing in a staging environment.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;* This projects reside inside a group in Gitlab called my-awesome-group-trigger&lt;/p&gt;

&lt;h2&gt;
  
  
  Pipelines
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Current pipeline flow:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb7kdo65csndeb1k5z21d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb7kdo65csndeb1k5z21d.png" alt="current-pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the current flow we have a stage to unit-testing, build our Docker image to later be deployed to production. We need to find a way to integrate this flow of the development team with the QA team and add some E2E tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  First desired pipeline flow:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foqlf9zn5nvgmx5gsxbfq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foqlf9zn5nvgmx5gsxbfq.png" alt="first-desired-pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this is a problem because the code related to our E2E tests would be in the same project that our production code, and this involves the two teams. So probably the best approach, in this case, is to have separate projects, this means we need to use a strategy to launch and sync our deployment pipelines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final pipeline flow:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw8d6ureq4v9nv4peyf0x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw8d6ureq4v9nv4peyf0x.png" alt="final-pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This would integrate the two projects and only when all the previous stages to deploy are successfully passed the code would be launched to production.&lt;/p&gt;

&lt;p&gt;* I would explain later what this &lt;code&gt;Bridge&lt;/code&gt; job is.&lt;/p&gt;

&lt;h2&gt;
  
  
  CI/CD without the integration
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;my-awesome-project/.gitlab-ci.yml
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image: alpine

stages:
    - test
    - build
    - deploy

test:
    stage: test
    script: 
        - echo "This is the test job."

build:
    stage: build
    script: 
        - echo "This is the build job."

deploy:
    stage: deploy
    script: 
        - echo "This is the deploy job."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;e2e-testing-qa/.gitlab-ci.yml
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image: alpine

stages:
    - deploy
    - e2e

deploy:
    stage: deploy
    script: 
        - echo "This is the deploy job."

e2e:
    stage: e2e
    script:         
        - echo "This is the E2E job."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we just need to make the QA integration, for this we are going to use the Multi-Project pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bridge Job:
&lt;/h2&gt;

&lt;p&gt;This is a job that has the special keyword &lt;code&gt;trigger&lt;/code&gt; and this automatically turns this job in a bridge job, this means that its only purpose is to launch a pipeline in another project or the same project (see &lt;a href="https://docs.gitlab.com/ee/ci/parent_child_pipelines.html" rel="noopener noreferrer"&gt;parent-child-pipelines&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Integration
&lt;/h2&gt;

&lt;p&gt;In this particular case we only need to modify the CI/CD file in &lt;code&gt;my-awesome-project&lt;/code&gt;, let's add the &lt;code&gt;bridge&lt;/code&gt; stage and job, between build and deploy to make our E2E testing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# my-awesome-project/.gitlab-ci.yml

image: alpine

stages:
    - test
    - build
    - bridge
    - deploy

test:
    stage: test
    script: 
        - echo "This is the test job."

build:
    stage: build
    script: 
        - echo "This is the build job."

bridge:
    stage: bridge
    trigger: my-awesome-group-trigger/e2e-testing-qa

deploy:
    stage: deploy
    script: 
        - echo "This is the deploy job."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see how our pipelines are working together (for the following images the top pipeline is the project &lt;code&gt;my-awesome-project&lt;/code&gt; and the bottom is &lt;code&gt;e2e-testing-qa&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Passing pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fplbqh9gdq0c3s9zzpe5q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fplbqh9gdq0c3s9zzpe5q.gif" alt="independent-pass-pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Falling pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fasjgs8zwbkutpp0eukj5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fasjgs8zwbkutpp0eukj5.gif" alt="independent-fail-pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Works! But it seems like the pipeline in the project &lt;code&gt;my-awesome-project&lt;/code&gt; it's not waiting for the &lt;code&gt;e2e-testing-qa&lt;/code&gt; to finish, and this is not our desired behavior for this case, remember we need to make the E2E tests before the production deployment. So we need to modify our &lt;code&gt;bridge&lt;/code&gt; job one more time to add the &lt;code&gt;strategy&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# my-awesome-project/.gitlab-ci.yml

...

bridge:
    stage: bridge
    trigger: my-awesome-group-trigger/e2e-testing-qa
    strategy: depend

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Passing pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F59cd346ogrnzr85ecg3m.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F59cd346ogrnzr85ecg3m.gif" alt="dependent-pass-pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Falling pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq4o447urdcgscjgf6m8u.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq4o447urdcgscjgf6m8u.gif" alt="dependent-fail-pipeline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, when the E2E testing has passed in the &lt;code&gt;e2e-testing-qa&lt;/code&gt; our deploy to production can be made automatically, and when it fails will also be falling our main pipeline.&lt;/p&gt;

</description>
      <category>gitlab</category>
      <category>cicd</category>
      <category>deploy</category>
      <category>e2e</category>
    </item>
    <item>
      <title>Setting up Pytest for a Django project</title>
      <dc:creator>Asdrubal Santander</dc:creator>
      <pubDate>Sat, 07 Dec 2019 21:40:04 +0000</pubDate>
      <link>https://dev.to/asdrubalsantander/setting-up-pytest-for-a-django-project-3362</link>
      <guid>https://dev.to/asdrubalsantander/setting-up-pytest-for-a-django-project-3362</guid>
      <description>&lt;p&gt;Hi, my name is Asdrubal Santander, I'm a Backend developer at Mo Tecnologías, and this is a short guide to how we implement testing with Pytest-Django.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Pytest?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.pytest.org/en/latest/index.html"&gt;Pytest&lt;/a&gt; it is an alternative framework to write tests in Python besides &lt;a href="https://docs.python.org/3/library/unittest.html"&gt;Unittest&lt;/a&gt;, between its best features it has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy to write &lt;code&gt;assert&lt;/code&gt; statements just like any other conditional &lt;code&gt;assert 1 == 1&lt;/code&gt;. No more assertNotNull, assertDictEquals...&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Fixtures&lt;/code&gt; to run code before and after each test, set up state of a test, load fixtures and more!&lt;/li&gt;
&lt;li&gt;Better integration with plugins and tools (like pdb, and coverage).&lt;/li&gt;
&lt;li&gt;Improved results, when a test fail it tells you exactly where it fails and with colors!&lt;/li&gt;
&lt;li&gt;Can run tests written with the syntax of Unittest. So it makes easy to change.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Our Project structure:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project_name
├── project
│   ├── apps
│   │   ├── integration_apps
│   │   |   ├── app_name
|   |   |   |   └── tests
|   |   |   |       ├── component_to_be_tested
|   |   |   |       |   ├── test_function_name.py
|   |   |   |       |
|   |   |   |       ├── conftest.py
|   |   |   |       └── ...
|   |   |   └── ...
|   |   └── ...
│   ├── project_settings
│   |   └── settings
|   |       ├── development_settings
|   |       ├── production_settings
|   |       └── test_settings
|   └── manage.py
├── pytest.ini
└── ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as you can see, our Django project as an uncommon structure, I will try to explain in a general way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;project_name&lt;/code&gt;: config files, requirements folder, README.MD ...&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;project&lt;/code&gt;: &lt;code&gt;apps&lt;/code&gt;, fixtures folder, &lt;code&gt;project_settings&lt;/code&gt; and &lt;code&gt;manage.py&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;project_settings&lt;/code&gt;: &lt;code&gt;settings&lt;/code&gt;, project urls.py, wsgi.py.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;settings&lt;/code&gt;: settings for our project in this case we are going to be focus on &lt;code&gt;test_settings&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;apps&lt;/code&gt;: contains our business logic in separated apps.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;test&lt;/code&gt;: inside this folder we have separated &lt;code&gt;conftest.py&lt;/code&gt; to an specific app, and is where we are going to put all our tests, separated by folders such like "views", "services", "selectors"...&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Installation:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pytest-django
pip install pytest-cov
pip install pytest-pythonpath
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pytest-django&lt;/code&gt;: A plugin to easily integrate Pytest with Django.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pytest-cov&lt;/code&gt;: Plugin that helps to create the coverage reports inside Pytest.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pytest-pythonpath&lt;/code&gt;: Plugin that modifies the env var PYTHONPATH. This is useful when pytest can't find your manage.py&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  pytest.ini:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[pytest]
addopts = -v -p no:warnings --nomigrations --cov=. --no-cov-on-fail
DJANGO_SETTINGS_MODULE = project.project_settings.settings.test_settings
python_paths = .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the file &lt;code&gt;pytest.ini&lt;/code&gt; we can set arguments and other configurations, so we don't have to specify each time we run Pytest.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;addopts&lt;/code&gt;: Special variable to set command line options&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-v&lt;/code&gt;: To tell pytest to be a little bit more verbose&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p no:warnings&lt;/code&gt;: Disable warnings.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--nomigrations&lt;/code&gt;: Disable the migrations when run pytest this could make the setup for testing faster.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--cov=.&lt;/code&gt;: When pytest runs it makes a coverage report.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--no-cov-on-fail&lt;/code&gt;: Don't show the coverage report if a test fails.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;DJANGO_SETTINGS_MODULE&lt;/code&gt;: This is where we tell pytest to run with a specific settings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;python_paths&lt;/code&gt;: The dot meaning that the current directory will be added to the PYTHONPATH env var.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Running Pytest:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pytest 

======================================== test session starts ========================================
platform darwin -- Python 3.6.0, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 -- 
...
cachedir: .pytest_cache
Django settings: project.project_settings.settings.test_settings (from ini file)
rootdir: .../mysite, inifile: pytest.ini
plugins: django-3.7.0, pythonpath-0.7.3, cov-2.8.1
collected 0 items



---------- coverage: platform darwin, python 3.6.0-final-0 -----------
Name                                                                Stmts   Miss  Cover
---------------------------------------------------------------------------------------
...
mo_services/apps/importantapp/test/services/test_function_name.py       0      0   100%
...
--------------------------------------------------------------------------------------------
TOTAL                                                                   X     X    XXX%


======================================= no tests ran in 0.09s =======================================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's write a example test:&lt;/p&gt;

&lt;p&gt;Inside of &lt;code&gt;app_name&lt;/code&gt; we create a python package named &lt;code&gt;tests&lt;/code&gt;, this package will have all the tests for this application divided in sub packages with the name of the component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .../apps/integration_apps/app_name/tests/component_to_be_tested/test_function_name.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;app_name.component_name&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;some_function&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestFunctionName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_example&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="n"&gt;expected_result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'some_result'&lt;/span&gt;

        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;expected_result&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running pytest again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pytest 
======================================== test session starts ========================================
platform darwin -- Python 3.6.0, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 -- 
...
cachedir: .pytest_cache
Django settings: project.project_settings.settings.test_settings (from ini file)
rootdir: .../mysite, inifile: pytest.ini
plugins: django-3.7.0, pythonpath-0.7.3, cov-2.8.1
collected 1 item

apps/integration_apps/app_name/tests/component_to_be_tested/test_function_name.py::TestFunctionName::test_example PASSED [100%]

--------------- coverage: platform darwin, python 3.6.0-final-0 -----------
Name                                                                Stmts   Miss  Cover
--------------------------------------------------------------------------------------------
...
mo_services/apps/importantapp/test/services/test_function_name.py       1      0   100%
...
--------------------------------------------------------------------------------------------
TOTAL                                                                   X     X    XXX%

========================================= 1 passed in 0.11s =========================================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now you have successfully run Pytest in Django!&lt;/p&gt;

&lt;h4&gt;
  
  
  Some final cool command line options:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-x&lt;/code&gt;: This option tells pytest to stop after the first test fail, is great when we are having problems with several tests so in this way we can solve this problems one by one.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--nf&lt;/code&gt;: Tells pytest to run the "new tests" first.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--ff&lt;/code&gt;: Tells pytest to run the "fail tests" first.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--lf&lt;/code&gt;: Tells pytest to run the only the "last fail".&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Where to find more:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.pytest.org/en/latest/index.html"&gt;pytest: helps you write better programs — pytest documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pythontesting.net/"&gt;Python Testing - Python Software Development and Software Testing (book, posts and podcast)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/41ek3VNx_6Q?list=PLPwQzG2-AU0VL688UCj1oQeCRPqtZB2ji"&gt;The Django Test Driven Development Cookbook - Singapore Djangonauts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>pytest</category>
    </item>
  </channel>
</rss>
