<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Maya Miftakhova on Medium]]></title>
        <description><![CDATA[Stories by Maya Miftakhova on Medium]]></description>
        <link>https://medium.com/@kotus9?source=rss-6937d187fb51------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*b3s4vs9a04shYw9vaLv06w.png</url>
            <title>Stories by Maya Miftakhova on Medium</title>
            <link>https://medium.com/@kotus9?source=rss-6937d187fb51------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 04:42:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kotus9/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to build Gitlab CI pipeline for python tests reporting to Allure TestOps]]></title>
            <link>https://medium.com/exness-blog/gitlab-ci-for-python-pytest-tests-reporting-to-allure-testops-28157c25fad4?source=rss-6937d187fb51------2</link>
            <guid isPermaLink="false">https://medium.com/p/28157c25fad4</guid>
            <category><![CDATA[test]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <category><![CDATA[qa]]></category>
            <dc:creator><![CDATA[Maya Miftakhova]]></dc:creator>
            <pubDate>Mon, 30 Jan 2023 10:08:49 GMT</pubDate>
            <atom:updated>2023-02-09T12:11:05.231Z</atom:updated>
            <content:encoded><![CDATA[<p>As a QA engineer at Exness, creating and maintaining CI pipelines for tests is a part of my day-to-day job. While it’s become routine for me now, writing my first Gitlab CI pipeline involved a lot of googling for answers.</p><p>I’ve created this guide to share my experience and hopefully help you with writing your own.</p><p>It explains code, dependencies, and environment variables used in a pipeline. After reading, you should be able to create a functional Gitlab CI pipeline that runs Python tests and reports results to Allure TestOps. This guide should answer any questions you might have along the way.</p><p>I hope you’ll have fun writing your first pipeline.</p><p>The issue we need to solve here is quite simple: we have some tests written in Python with pytest. There is pipenv that is being used as a dependency manager.</p><ul><li>We need to run our tests in Gitlab CI,</li><li>The test results should be collected and reported to Allure TestOps.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*ScV8l-GuRkLgbREw_m0GYA.png" /></figure><p><strong>A brief look at the test code</strong></p><p>Let’s take a look at how we are going to install dependencies and run tests in CI.</p><p>In our code we use <a href="https://pypi.org/project/pipenv/">pipenv</a> as a dependency manager. It means there are Pipfile and Pipfile.lock files that store information about dependencies used in the project.</p><p>Minimum packages declared in our Pipfile are:</p><pre>[packages]<br>pytest = &quot;*&quot;<br>allure-pytest = &quot;*&quot;</pre><p>Here pytest is our test framework. And <a href="https://pypi.org/project/allure-pytest/">allure-pytest</a> is a package that generates the test report we are going to send to Allure TestOps.</p><p>In addition to defining dependencies, we could add scripts to the [scripts] section of the Pipfile. It’s super handy to run tests, linters, or any other useful scripts.</p><p>In our case the [scripts] section contains tests_ci script that we will use in the pipeline later.</p><pre>[scripts]<br>tests_ci = &quot;pytest --alluredir=allure_results&quot;</pre><p>As you can see, the only difference from just running pytest is using --alluredir=allure_results. It tells allure-pytest package to generate report files and save them in the allure_results folder.</p><p>In CI we are going to install pipenv and python dependencies using the following commands:</p><pre>- pip install pipenv<br>- pipenv sync</pre><p>pipenv sync will install all dependencies from Pipfile.lock. As a result, we can be sure we get a reproducible environment.</p><p>And this is the command we are going to use to run tests:</p><pre>- pipenv run tests_ci</pre><p><strong>How to tell Gitlab CI to run a pipeline?</strong></p><p>Fortunately, it is pretty straightforward: all you need is to create a gitlab-ci.yml file in the root of your repo! Gitlab will check the file’s contents and run a pipeline.</p><p>CI script is a set of instructions that describe what, when and how should be done when a pipeline runs.</p><p>A pipeline consists of stages and jobs described within those stages. In our case, we will only have one stage and one job to run our tests.</p><p>Here is what we can write so far:</p><pre>stages:<br>  - tests<br><br>test:<br>  stage: tests<br>  <br>  script:<br>    - pip install pipenv<br>    - pipenv sync<br>    - pipenv run tests_ci</pre><p>While this script describes that we need to install dependencies and run tests, it is not enough. We need to add some instructions of where to run our code.</p><p><strong>Where tests will run: the runner and the tag</strong></p><p>tags keyword in gitlab-ci.yml expects a list of runners where the job should run.</p><p>We already have a runner with the tag test_tag we are going to use in our pipeline:</p><pre>  tags:<br>    - test_tag</pre><blockquote>In case you don’t have your own runner set up, you can use shared Gitlab CI runners. In Gitlab web interface go to Settings -&gt; CI/CD -&gt; Runners -&gt; Expand to choose one.</blockquote><p><strong>Docker image for tests</strong></p><p>Now we know where our tests will run, it’s time to pick a docker image! The image keyword is exactly about that.</p><p>We are going to simply use python:3.10, as it fits our needs perfectly. In a Gitlab script, it would look like this:</p><pre>image: python:3.10</pre><p>Let’s see what the updated version of the script looks like:</p><pre>stages:<br>  - tests<br><br>test:<br>  image: python:3.10 <br><br>  tags:<br>    - test_tag<br>  <br>  stage: tests<br><br>  script:<br>    - pip install pipenv<br>    - pipenv sync<br>    - pipenv run tests_ci</pre><p>This script should already be working!</p><h3>Send report to Allure TestOps</h3><p>We are almost there. Let’s find out what we need to post test results to <a href="https://qameta.io/">Allure TestOps</a>.</p><h4>1. The report itself.</h4><p>Fortunately, it is already generated by allure-pytest package.</p><h4>2. <strong>allurectl</strong> — command line tool for Allure TestOps, that will upload the report.</h4><p>We are going to download the latest version each time the job runs:</p><pre>- wget https://github.com/allure-framework/allurectl/releases/latest/download/allurectl_linux_386 -O /usr/bin/allurectl<br>- chmod +x /usr/bin/allurectl</pre><blockquote>Alternatively, it is possible to create an image with <em>allurectl</em> preinstalled, or to keep the tool somewhere in the project.</blockquote><p>To upload the test results we will use the following command:</p><pre>- allurectl upload $TEST_REPORT_DIR</pre><p>where $TEST_REPORT_DIR is the folder with the test report.</p><h4>3. Some<strong> environment variables</strong> set <strong>in the pipeline</strong>:</h4><pre>variables:<br>  ALLURE_LAUNCH_NAME: &quot;${CI_PROJECT_NAME}_${CI_JOB_ID}&quot;<br>  ALLURE_JOB_RUN_UID: &quot;${CI_JOB_ID}&quot;</pre><p>ALLURE_LAUNCH_NAME — name of the test run in Allure TestOps. We combine it from the Gitlab project name and unique job id.</p><p>ALLURE_JOB_RUN_UID — is a job run identifier inside Allure. We define it as CI_JOB_ID — a unique identifier of the Gitlab job that is generated each time we run a job.</p><blockquote>Defining unique ALLURE_JOB_RUN_UID is optional.</blockquote><blockquote>If it is not set in CI, test reports for several jobs in one pipeline will be combined into one Allure report.</blockquote><blockquote>If <em>ALLURE_JOB_RUN_UID </em>is unique, then each job inside one pipeline will have a separate test run in Allure TestOps.</blockquote><h4>4. Several <strong>environment variables</strong> set <strong>in the Gitlab’s Project settings</strong>.</h4><blockquote>Setting environment variables for the project: Settings -&gt; CI/CD -&gt; Variables -&gt; Expand.</blockquote><pre>ALLURE_ENDPOINT=&quot;https://your_allure_testops.com&quot;<br>ALLURE_PROJECT_ID=33<br>ALLURE_TOKEN=&quot;very_secure_token&quot;</pre><p>ALLURE_ENDPOINT — the Allure TestOps URL address.</p><p>ALLURE_PROJECT_ID — Allure project identifier. The test run will appear in the project specified here. This ID can be found in the Allure URL: https://your_allure_testops.com/project/13/dashboards — in this URL it is 13.</p><p>ALLURE_TOKEN — is the token that allows posting data to Allure TestOps. It can be created in Allure.</p><blockquote>To create Allure TestOps token, go to your Allure TestOps web interface, click user avatar in the left lower corner of the screen -&gt; Your profile -&gt; API tokens -&gt; Create</blockquote><p>Why is it better to store some environment variables in project settings and not in the pipeline script?</p><p>- There is no need to change them often.</p><p>- They contain sensitive data: passwords, tokens and so on. Storing them in Project settings allows you to mark them as “masked”, and job logs won’t contain this data.</p><p>- If variables are stored on a group level — they can be reused in different projects.</p><p>Let’s take a look at a few more things before creating the final gitlab-ci.yml script.</p><p><strong>rules and </strong><strong>allow_failure</strong></p><p>Gitlab job can contain some rules that define how and when the job is triggered.</p><pre>rules:<br>    - if: &#39;($CI_PIPELINE_SOURCE == &quot;push&quot;) || ($CI_PIPELINE_SOURCE == &quot;merge_request_event&quot;)&#39;<br>      when: manual<br>    - when: always<br><br>  allow_failure: false</pre><p>rules defined here work the following way:</p><ul><li>If the pipeline is created as a result of push <em>or</em> merge request — test job won’t run automatically. It will wait for manual user action.</li><li>In all other cases — test job will run automatically.</li></ul><p>According to <a href="https://docs.gitlab.com/ee/ci/yaml/#allow_failure">Gitlab CI docs</a> allow_failure is true for manual jobs by default. In our case, it means the pipeline won’t fail regardless of job’s exit code. By the way, we want either a red or green pipeline — so we do not allow failures.</p><p><strong>before_script, </strong><strong>script, </strong><strong>after_script</strong></p><p>These are essential parts of the job defining strict order of actions and describing what should be actually done.</p><pre>before_script:<br>    - wget https://github.com/allure-framework/allurectl/releases/latest/download/allurectl_linux_386 -O /usr/bin/allurectl<br>    - chmod +x /usr/bin/allurectl<br>    - pip install pipenv<br>    - pipenv sync --dev<br>  <br>  script:<br>    - pipenv run tests_ci<br>  <br>  after_script:<br>    - allurectl upload $TEST_REPORT_DIR</pre><p>The before_script section is generally used to install dependencies and make some preparations. We’ve installed everything we need in this section.</p><p>script is the main part of the job — if this part finishes running with errors — the whole job will be marked as failed. Only tests are run in this section.</p><p>Commands described in after_script will run regardless of the script exit code, therefore this part is great for reports, notifications to chats and so on.</p><p><strong>The final script</strong></p><p>Finally, we can combine all the items and get a shiny working CI.</p><pre>stages:<br>  - tests<br><br>variables:<br>  ALLURE_LAUNCH_NAME: &quot;${CI_PROJECT_NAME}_${CI_JOB_ID}&quot;<br>  ALLURE_JOB_RUN_UID: &quot;${CI_JOB_ID}&quot;<br>  TEST_REPORT_DIR: &quot;allure_results&quot;<br><br>test:<br>  image: python:3.10 <br><br>  tags:<br>    - test_tag<br>  <br>  stage: tests<br>  <br>  rules:<br>    - if: &#39;($CI_PIPELINE_SOURCE == &quot;push&quot;) || ($CI_PIPELINE_SOURCE == &quot;merge_request_event&quot;)&#39;<br>      when: manual<br>    - when: always<br>  <br>  allow_failure: false<br>  <br>  before_script:<br>    - wget https://github.com/allure-framework/allurectl/releases/latest/download/allurectl_linux_386 -O /usr/bin/allurectl<br>    - chmod +x /usr/bin/allurectl<br>    - pip install pipenv<br>    - pipenv sync<br>    <br>  script:<br>    - pipenv run tests_ci<br>    <br>  after_script:<br>    - allurectl upload $TEST_REPORT_DIR<br></pre><p>This pipeline has only one stage tests, and one job test belonging to that stage.</p><p>The test job consists of defining image, tags, stage, rules, and blocks describing what should be done before, during and after the script.</p><p>And that’s it! I hope this guide covers all your CI pipeline creation needs and saves you precious time on running Python tests.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28157c25fad4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/exness-blog/gitlab-ci-for-python-pytest-tests-reporting-to-allure-testops-28157c25fad4">How to build Gitlab CI pipeline for python tests reporting to Allure TestOps</a> was originally published in <a href="https://medium.com/exness-blog">Exness Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>