简体   繁体   English

使用GitLab CI进行单元测试覆盖

[英]Unit test coverage with GitLab CI

I'm trying to set up a unit test coverage tool for one of the Python projects. 我正在尝试为其中一个Python项目设置单元测试覆盖率工具。 I managed to script the GitLab CI YML file, but it runs into errors when triggered. 我设法编写了GitLab CI YML文件的脚本,但是在触发时会遇到错误。 Here is the error that I get: 这是我得到的错误:

ImportError while importing test module '/builds/user1/myProj/tests/test_run.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_run.py:12: in <module>
    from data import prepare_data as pd
E   ImportError: No module named 'data'
=========================== 1 error in 0.06 seconds ============================

In the test_run.py , I have the following imports: test_run.py ,我有以下导入:

sys.path.append(os.path.abspath('../src'))
from data import prepare_data as pd

Here is my GitLab CI YML file: 这是我的GitLab CI YML文件:

- pip install -r requirements.txt
- python -m pytest -v --cov=myproj/ --cache-clear --color=yes tests/

I have the needed __init__.py in the src folder. 我在src文件夹中有所需的__init__.py The src folder has a sub folder called data which I'm importing in my unit test as: src文件夹有一个名为data的子文件夹,我在单元测试中导入:

sys.path.append(os.path.abspath('../src'))

Is there something that I'm missing? 有什么东西我不见了吗?

I managed to fix this error by completely moving to another test coverage Python package. 我设法通过完全转移到另一个测试覆盖Python包修复此错误。 Here is my GitLab CI yml configuration that prints the coverage report upon even run by GitLab: 这是我的GitLab CI yml配置,它甚至在GitLab运行时打印覆盖率报告:

- nosetests --with-coverage --cover-erase --cover-package=tests/ --cover-html

This just works like I expected it to be! 这就像我预期的那样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM