我正在为 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
完全相同的响应(没有 tensorflow 日志):
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
。我运行了这个,但果然,要求已经满足
。
如何执行 keras pytests。 我正在运行 ubuntu 18.04.1、python 3.6.5 和 anaconda 64 位。
最佳答案
Contributing 中提到了运行 keras
测试页:
Run our test suite locally. It's easy: from the Keras folder, simply run:
py.test tests/
.You will need to install the test requirements as well:
pip install -e .[tests]
.
如果您不想在可编辑模式下安装包,只需显式安装所有测试依赖项即可。 Looking at keras
' setup script ,该命令将是:
$ pip install pytest pytest-pep8 pytest-xdist pytest-cov pytest-timeout pandas requests
关于python - 如何为keras源代码运行pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839911/