我在travisci端在docker上执行pytest命令时遇到问题。执行命令后:
docker exec my_package pytest --cov=/usr/local/lib/python3.7/site-packages/my_package tests/ -vvv
以下错误可见:
tests/test_views/test_users.py::test_get_users PASSED [100%]
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 209, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 249, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 68, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 62, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 203, in _multicall
INTERNALERROR> gen.send(outcome)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pytest_cov/plugin.py", line 229, in pytest_runtestloop
INTERNALERROR> self.cov_controller.finish()
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/pytest_cov/engine.py", line 167, in finish
INTERNALERROR> self.cov.stop()
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/coverage/control.py", line 782, in save
INTERNALERROR> self.data_files.write(self.data, suffix=self.data_suffix)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/coverage/data.py", line 680, in write
INTERNALERROR> data.write_file(filename)
INTERNALERROR> File "/usr/local/lib/python3.7/site-packages/coverage/data.py", line 467, in write_file
INTERNALERROR> with open(filename, 'w') as fdata:
INTERNALERROR> PermissionError: [Errno 13] Permission denied: '/code/.coverage'
此问题在travisci上是永久性的。但是奇怪的是,相同的命令在本地docker容器上起作用。我无法在Dockerfile中使用root用户,原因是我的测试很少需要非授权用户。
.travis.yml
---
dist: xenial
services:
- docker
language: python
python:
- "3.7"
script:
- docker-compose up --build -d
- docker exec my_package_container pytest --cov=/usr/local/lib/python3.7/site-packages/my_package tests/ -vvv
- docker exec my_package_container coveralls
- docker-compose down
Dockerfile
FROM python:3.7
WORKDIR /code
RUN apt-get update && apt-get install -y musl-dev gcc postgresql
ADD requirements-test.txt /code/
RUN pip3 -r requirements-test.txt
COPY . /code
RUN pip3 install .
EXPOSE 5000
RUN useradd user
USER user