5

我当前的工作流程是在 Travis CI 上测试的 github PR 和构建,使用 tox 测试 pytest 并将覆盖率报告给 codeclimate。

travis.yml

os:
- linux
sudo: false
language: python
python:
- "3.3"
- "3.4"
- "3.5"
- "pypy3"
- "pypy3.3-5.2-alpha1"
- "nightly"
install: pip install tox-travis
script: tox

毒物

[tox]
envlist = py33, py34, py35, pypy3, docs, flake8, nightly, pypy3.3-5.2-alpha1

[tox:travis]
3.5 = py35, docs, flake8

[testenv]
deps = -rrequirements.txt
platform =
    win: windows
    linux: linux
commands =
    py.test --cov=pyCardDeck --durations=10 tests

[testenv:py35]
commands =
    py.test --cov=pyCardDeck --durations=10 tests
    codeclimate-test-reporter --file .coverage
passenv =
    CODECLIMATE_REPO_TOKEN
    TRAVIS_BRANCH
    TRAVIS_JOB_ID
    TRAVIS_PULL_REQUEST
    CI_NAME

但是,Travis 没有为拉取请求传递我的环境变量,这使得我的覆盖率报告失败。Travis 文档将此显示为解决方案:

script:
   - 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then bash ./travis/run_on_pull_requests; fi'
   - 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./travis/run_on_non_pull_requests; fi'

但是,在 tox 中,这不起作用,因为 tox 正在使用 subprocess python 模块并且不识别 if 作为命令(自然)。

如何仅为构建而不是基于 TRAVIS_PULL_REQUEST 变量的拉取请求运行 codeclimate-test-reporter?我必须创建自己的脚本并调用它吗?有更聪明的解决方案吗?

4

2 回答 2

1

您可以有两个tox.ini文件并从中调用travis.yml

script: if [ $TRAVIS_PULL_REQUEST ]; then tox -c tox_nocodeclimate.ini; else tox -c tox.ini; fi

于 2016-09-18T20:03:56.397 回答
0

我的解决方案是通过 setup.py 命令处理一切

毒物.ini

[testenv:py35]
commands =
    python setup.py testcov
passenv = ...

安装程序.py

class PyTestCov(Command):
    description = "run tests and report them to codeclimate"
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        errno = call(["py.test --cov=pyCardDeck --durations=10 tests"], shell=True)
        if os.getenv("TRAVIS_PULL_REQUEST") == "false":
            call(["python -m codeclimate_test_reporter --file .coverage"], shell=True)
        raise SystemExit(errno)

 ...


  cmdclass={'testcov': PyTestCov},
于 2016-09-23T14:38:15.553 回答