pytest KeyError:' USER'在docker container

时间:2018-04-30 18:31:58

标签: python docker pytest

我正在构建一个将运行自动化测试的docker容器。当我尝试运行下面的命令[这在本地和jenkins中工作]时,我收到以下错误:

root@645ed3930434:/test_dir# pytest test_* -m smoke
Traceback (most recent call last):
  File "/usr/local/bin/pytest", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 49, in main
    config = _prepareconfig(args, plugins)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 168, in _prepareconfig
    pluginmanager=pluginmanager, args=args)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 745, in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 613, in execute
    return _wrapped_call(hook_impl.function(*args), self.execute)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 250, in _wrapped_call
    wrap_controller.send(call_outcome)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/helpconfig.py", line 68, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 279, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 265, in __init__
    self.result = func()
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
    res = hook_impl.function(*args)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 957, in pytest_cmdline_parse
    self.parse(args)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 1121, in parse
    self._preparse(args, addopts=addopts)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 1084, in _preparse
    self.pluginmanager.load_setuptools_entrypoints('pytest11')
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 516, in load_setuptools_entrypoints
    self.register(plugin, name=ep.name)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 264, in register
    ret = super(PytestPluginManager, self).register(plugin, name)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 371, in register
    hook._maybe_apply_history(hookimpl)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 768, in _maybe_apply_history
    res = self._hookexec(self, [method], kwargs)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 339, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 334, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/usr/local/lib/python3.5/dist-packages/_pytest/vendored_packages/pluggy.py", line 614, in execute
    res = hook_impl.function(*args)
  File "/usr/local/lib/python3.5/dist-packages/pytest_cloud/plugin.py", line 76, in pytest_addoption
    os.environ['USER'],
  File "/usr/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None
KeyError: 'USER'

我是docker的新手,所以我不明白导致此错误的原因。 我开始以ubuntu作为我的基础[当使用https://hub.docker.com/_/python/作为我的基础时也会发生这种情况]

参考requirements.txt包括以下内容:

apipkg==1.4
certifi==2017.11.5
chardet==3.0.4
execnet==1.5.0
idna==2.6
parameterized==0.6.1
psutil==5.4.1
py==1.5.2
pytest==3.2.5
pytest-cloud==2.0.0
pytest-forked==0.2
pytest-xdist==1.20.1
requests==2.18.4
six==1.11.0
timeout-decorator==0.4.0
urllib3==1.22

我已在容器中安装了Python 3.5.2

1 个答案:

答案 0 :(得分:3)

&#39;用户&#39;环境变量似乎没有在您的图像中定义。在外部定义它,或使你的代码适应它的缺席:

  • 在您的代码中,使用os.getenv('USER', backup_user_name)代替os.environ['USER']
  • 在您的代码use getpass中,而不是依靠您的环境来获取当前用户。
import getpass
print getpass.getuser()
  • docker run中,您可以添加-e USER=$(whoami)或类似的内容
  • docker-compose中,您可以在该服务的USER: some_user_name设置下添加environ