Sonarqube zero coverage with python tests - python

I am trying to run sonarqube in a gitlab pipeline and pytests, and it does not return coverage.
Seems it finds the coverage file, according to the logs, but shows 0% coverage.
I am quite desperate as tried multiple solutions and combinations already.
Gitlab pipeline is (where commented out things is what I ran with/without for tests)
Unit tests:
image: python:3.9-slim
stage: test
before_script:
- python3 -V
- pip install --upgrade setuptools
- pip install ez_setup
# - pip install unroll
# - pip install -r requirements.txt
- pip install pytest pytest-cov
- pip install pytest
- pip install pytest-metadata
script:
- export PYTHONUNBUFFERED=1
# - python3 -m pytest
# - coverage run -m pytest
# - coverage report
# - coverage run -m pytest -rap --junitxml coverage.xml
# - coverage xml -i
- pytest -v --cov --cov-report=xml --cov-report=html
# - coverage lcov
- python3 -V
- ls -a
coverage: /All\sfiles.*?\s+(\d+.\d+)/
artifacts:
# reports:
# cobertura: cobertura-coverage.xml
paths:
# - coverage.lcov
- coverage.xml
- .coverage
only:
- merge_requests
- master
- development
sonarqube-check:
stage: analysis
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- ls -a
- ls -a .coverage
- sonar-scanner -X
allow_failure: true
only:
- merge_requests
- main
- main
sonar-project file
sonar.projectKey=XXXXX
sonar.qualitygate.wait=true
sonar.language=py
sonar.python.version=3.9
sonar.projectVersion=1.0
sonar.core.codeCoveragePlugin=cobertura
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.xunit.reportPaths=coverage.xml
sonar.verbose=true
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=tests/*.py, src/*.py
Folder structure is just 2 folders tests and src, with .py files in each.
Logs are
16:08:59.221 INFO: Sensor Cobertura Sensor for Python coverage [python]
16:08:59.221 DEBUG: Using pattern 'coverage.xml' to find reports
16:08:59.251 INFO: Python test coverage
16:08:59.255 INFO: Parsing report '/correctpath/coverage.xml'
16:08:59.373 DEBUG: 'src/delta.py' generated metadata as test with charset 'UTF-8'
16:08:59.376 DEBUG: 'src/invoice.py' generated metadata as test with charset 'UTF-8'
16:08:59.383 DEBUG: 'src/portfolio.py' generated metadata as test with charset 'UTF-8'
16:08:59.395 DEBUG: Saving coverage measures for file 'src/p1.py'
16:08:59.420 DEBUG: Saving coverage measures for file 'src/__init__.py'
16:08:59.424 DEBUG: 'src/__init__.py' generated metadata as test with charset 'UTF-8'
16:08:59.425 DEBUG: Saving coverage measures for file 'src/invoice.py'
16:08:59.426 DEBUG: Saving coverage measures for file 'src/delta.py'
16:08:59.428 INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=207ms
16:08:59.429 INFO: Sensor JaCoCo XML Report Importer [jacoco]
16:08:59.435 INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
16:08:59.436 INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
Pipelines pass, but coverages are 0%.
I tried both coverage and pytest libraries, in case one of them has wrong format of coverage.xml
Thanks for any help!

Related

pytest coverage report missing code in parallel gitlab pipelines' test execution

I am trying to set up test coverage and generate reports for sonarqube, however, there are some inconsistencies with my results.
I have several steps defined in my gitlab-ci.yml file to run tests in parallel, that generate a single coverage report:
.run_warehouse_tests: &run_warehouse_tests |
echo "Running tests for Warehouse"
python -m pytest --durations=0 ./src/unittest -m eagle --junitxml="junit-test-result-warehouse.xml" --cov-config=.coveragerc --cov-report xml --cov=./src/main --cov-append
coverage lcov
.run_logistics_tests: &run_logistics_tests |
echo "Running tests for Logistics"
python -m pytest --durations=0 ./src/unittest -m eagle --junitxml="junit-test-result-logistics.xml" --cov-config=.coveragerc --cov-report xml --cov=./src/main --cov-append
coverage lcov
.run_packaging_tests: &run_packaging_tests |
echo "Running tests for Packaging"
python -m pytest --durations=0 ./src/unittest -m eagle --junitxml="junit-test-result-packaging.xml" --cov-config=.coveragerc --cov-report xml --cov=./src/main --cov-append
coverage lcov
.
.
.
test_warehouse:
stage: test
tags:
- dind
script:
- *set_common_env_vars
- *setup_venv
- *run_warehouse_tests
artifacts:
when: always
paths:
- infrastructure/junit-test-result-warehouse.xml
- infrastructure/coverage.xml
- infrastructure/coverage.lcov
except:
- tags
test_logistics:
stage: test
tags:
- dind
script:
- *set_common_env_vars
- *setup_venv
- *run_logistics_tests
artifacts:
when: always
paths:
- infrastructure/junit-test-result-logistics.xml
- infrastructure/coverage.xml
- infrastructure/coverage.lcov
except:
- tags
test_packaging:
stage: test
tags:
- dind
script:
- *set_common_env_vars
- *setup_venv
- *run_packaging_tests
artifacts:
when: always
paths:
- infrastructure/junit-test-result-packaging.xml
- infrastructure/coverage.xml
- infrastructure/coverage.lcov
except:
- tags
I also have a .coveragerc file:
[run]
parallel = true
However, at the moment the report that gets generated misses some code. For some of the files the coverage is shown only for imports and function definitions, but not the functions' code itself.
Does anyone have experience with this and has an idea of why some code is not covered? I am thinking this might be because of the parallel execution in gitlab pipelines and maybe some tests are finishing before others? Although I'm using --cov-append to make sure that only one report is generated...
I have tried various things, like using coverage instead of pytest, trying to configure tox.ini, setting various flags in the pytest command, but so far no success.

No data in Allure report

I am testing the application within the GitLab pipeline. There is such a script:
stages:
- test
- report
run_ui_tests:
tags:
- est
stage: test
before_script:
- echo "Prepairing enviroment..."
- python --version
- pip install -r requirements.txt
script:
- echo "Executing ui tests with Pytest..."
- cd cio_tests
- dir
- pytest -v authorize_test.py
allow_failure: true
artifacts:
when: always
paths:
- cio_tests/allure-results/
expire_in: 5 mins 30 sec
reporting:
tags:
- est
stage: report
needs:
- run_ui_tests
script:
- cd cio_tests
- dir
- allure generate --clean cio_tests/allure-report/
artifacts:
when: always
paths:
- cio_tests/allure-report/
expire_in: 5 days
The pipeline ends successfully, and Allure report is saved locally on disk. However, when the report is called in the browser, there is no data in it:
What's wrong?
Solution found. To open the built report locally, you need to raise the server in the directory with the allure-report folder.
For this :
Go to the directory with the allure-report folder in cmd or shell
Run the command: allure open -p allure-report
Allure report will be open in Edge browser

Pipeline job fails after tests complete

There is job script:
image: python:3.7.9
stages:
- test
run_ui_tests:
tags:
- est
stage: test
before_script:
- echo "Prepairing enviroment..."
- python --version
- pip install -r requirements.txt
script:
- echo "Executing ui tests with Pytest..."
- cd cio_tests
- pytest -v authorize_test.py
after_script:
- echo "Cleaning test catalogue..."
job fails after all tests are completed:
What is the reason for this behaviour? After all, the tests are completed and one of the tests found a bug

Publish Python project code coverage with pytest in Azure Pipelines

I am trying to pblish code coverage results on the pipeline run summary page. This is my pipeline.yaml file:
- bash: |
pip install .[test]
pip install pytest pytest-azurepipelines pytest-cov
pytest --junitxml=junit.xml --cov=./src_dir --cov-report=xml --cov-report=html tests
displayName: Test
- task: PublishCodeCoverageResults#1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov'
The coverage report keeps showing 0% always
How to get the correct code coverage results?
Thanks!
I was able to get the correct code coverage using the following in my Azure pipeline "Test" stage:
echo "
[run]
source =
$(Build.Repository.Name)" > .coveragerc
coverage run --context=mytest -m pytest -v -rA --junitxml=junit.xml --rootdir=tests
coverage report -m --context=mytest

How to get detailed error information when gitlab-ci fails

Gitlab version is 13.6.6
Gitlab-runner version is 11.2.0
my .gitlab-ci.yml:
image: "python:3.7"
before_script:
- pip install flake8
flake8:
stage: test
script:
- flake8 -max-line-length=79
tags:
- test
The only information obtained from Pipelines is script failure and the output of failed job is No job log. How can I get more detailed error output?
Using artifacts can help you.
image: "python:3.7"
before_script:
- pip install flake8
flake8:
stage: test
script:
- flake8 -max-line-length=79
- cd path/to
tags:
- test
artifacts:
when: on_failure
paths:
- path/to/test.log
The log file can be downloaded via the web interface.
Note:- Using when: on_failure will ensure that test.log will only be collected if the build fails, saving disk space on successful builds.

Categories

Resources