如何为keras运行pytest源代码

时间:2018-10-16 16:20:49

标签: python keras anaconda travis-ci pytest

我正在为Keras框架开发自定义层,并希望扩展其现有的测试套件。

因为我正在研究keras源代码,所以我已经这样安装了它:

  • pip install keras-preprocessing
  • pip install keras-applications
  • git clone https://github.com/keras-team/keras
  • cd keras
  • export PYTHONPATH=$PWD:$PYTHONPATH

执行完这些命令后,您就在克隆的keras存储库中,这是下面的代码示例的工作目录。

首先,我想运行现有的测试并查看它们的作用。 It looks like它们可以简单地作为python文件运行:

if __name__ == '__main__':
    pytest.main([__file__])

但是这个:

python tests/keras/layers/wrappers_test.py

产生以下输出,并且不运行任何测试

Using TensorFlow backend.
usage: wrappers_test.py [options] [file_or_dir] [file_or_dir] [...]
wrappers_test.py: error: unrecognized arguments: -n tests/keras/layers/wrappers_test.py
  inifile: /home/lhk/programming/keras/pytest.ini
  rootdir: /home/lhk/programming/keras

所以我尝试了显式地调用pytest:

pytest tests/keras/layers/wrappers_test.py

完全相同的响应(没有张量流日志):

usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: -n tests/keras/layers/wrappers_test.py
  inifile: /home/lhk/programming/keras/pytest.ini
  rootdir: /home/lhk/programming/keras

我正在使用PyCharm,并且如果我从pycharm(我承认首先尝试过)中运行测试,那么它只会产生上面的消息。

显然,我没有正确配置它。 Pytest无法使用测试套件。为了找到参考配置,我查看了Keras CI设置。他们使用Travis,并且配置是开源的:https://travis-ci.org/keras-team/keras/jobs/442252422/config

看起来我已经安装了所有依赖项。实际的测试命令基本上就是我已经尝试过的:

PYTHONPATH=$PWD:$PYTHONPATH py.test tests/ --ignore=tests/integration_tests --ignore=tests/test_documentation.py --ignore=tests/keras/legacy/layers_test.py --cov-config .coveragerc --cov=keras tests/

它产生与上面完全相同的输出。

我认为问题出在pytest。但是他们的自动化测试的安装部分仅显示pip install pytest pytest-pip8。我执行了此操作,但肯定是requirement already satisfied

如何执行keras pytests。 我正在使用anaconda 64位运行ubuntu 18.04.1,python 3.6.5。

1 个答案:

答案 0 :(得分:1)

Contributing页面上提到了运行keras测试:

  

在本地运行我们的测试套件。很简单:只需从Keras文件夹中运行:py.test tests/

     

您还将需要安装测试要求:pip install -e .[tests]

如果您不想以可编辑模式安装软件包,只需显式安装所有测试依赖项即可。 Looking at keras' setup script,其命令为:

$ pip install pytest pytest-pep8 pytest-xdist pytest-cov pytest-timeout pandas requests