pytest:`foo is Enum.FOO` 在本地测试 True,在 travis-ci 上测试 False
pytest: `foo is Enum.FOO` tests True locally, False on travis-ci
晕!我在检查枚举的相等性时遇到奇怪的问题。我的本地测试通过了,但在 Travis 上却失败了。这是 Travis 的测试失败示例:
_____________________________ test_extract_object ______________________________
def test_extract_object():
"""Test UmlCreator.extract_object()."""
creator = UmlCreator(".")
with pytest.raises(ValueError):
creator.extract_object(["no", "klass", "included"])
obj = creator.extract_object(
[
"public",
"static",
"class",
"StaticKlass",
":",
"ICanBeImplemented,",
"IComparable,",
"IEquatable<Klass>",
]
)
assert obj.__class__.__name__ == "UmlClass"
> assert obj.access is Access.PUBLIC
E assert Access("public") is Access("public")
E + where Access("public") = UmlClass(['StaticKlass', ':', 'ICanBeImplemented,', 'IComparable,', 'IEquatable<Klass>'], **{'nsp': None, 'access': Access("public"), 'attrs': [], 'modifiers': [Modifier("static")], 'repo_url': None}).access
E + and Access("public") = Access.PUBLIC
tests/test_creator.py:44: AssertionError
如果我将有问题的行更改为 assert obj.access.value == Access.PUBLIC.value
,它也会传递给 Travis,但我很困惑为什么它在一个地方失败并通过基本相同的设置在另一个地方传递,并且它的当然也会影响 non-test 代码,这意味着我在代码中使用 foo is Enum.VAL
的任何地方,对这些方法的测试也会失败:
tests/test_creator.py:44: AssertionError
_________________________ test_uml_class_display_name __________________________
def test_uml_class_display_name():
"""Test UmlClass.display_name()."""
klass = UmlClass(["Classy"])
assert klass.display_name() == "Classy"
abstract_klass = UmlClass(["Classy"], modifiers=[Modifier.ABSTRACT])
> assert abstract_klass.is_abstract()
E assert False
本地
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
$ pip list
Package Version
------------------ --------
appdirs 1.4.3
astroid 2.3.3
attrs 19.3.0
bandit 1.6.2
black 19.10b0
Click 7.0
coverage 5.0.3
gitdb2 2.0.6
GitPython 3.0.7
importlib-metadata 1.5.0
isort 4.3.21
lazy-object-proxy 1.4.3
mccabe 0.6.1
more-itertools 8.2.0
packaging 20.1
pathspec 0.7.0
pbr 5.4.4
pip 20.0.2
pluggy 0.13.1
py 1.8.1
pylint 2.4.4
pyparsing 2.4.6
pytest 5.3.5
pytest-cov 2.8.1
PyYAML 5.3
regex 2020.1.8
rope 0.16.0
setuptools 45.1.0
six 1.14.0
smmap2 2.0.5
stevedore 1.32.0
toml 0.10.0
typed-ast 1.4.1
wcwidth 0.1.8
wheel 0.34.2
wrapt 1.11.2
zipp 2.2.0
$ pipenv run test
===== test session starts =====
platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /mnt/c/Users/thy/source/repos/uml.cs, inifile: pytest.ini
plugins: cov-2.8.1
collected 41 items
特拉维斯
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic
$ pip list
Package Version
------------------ ----------
appdirs 1.4.3
astroid 2.3.3
atomicwrites 1.3.0 !
attrs 19.3.0
bandit 1.6.2
black 19.10b0
certifi 2019.6.16 !
Click 7.0
coverage 5.0.3
gitdb2 2.0.6
GitPython 3.0.7
importlib-metadata 1.5.0
isort 4.3.21
lazy-object-proxy 1.4.3
mccabe 0.6.1
mock 3.0.5 !
more-itertools 8.2.0
nose 1.3.7 !
numpy 1.18.0 !
packaging 20.1
pathspec 0.7.0
pbr 5.4.4
pip 19.3.1
pipenv 2018.11.26
pluggy 0.13.1
py 1.8.1
pylint 2.4.4
pyparsing 2.4.6
pytest 5.3.5
pytest-cov 2.8.1
PyYAML 5.3
regex 2020.1.8
rope 0.16.0
setuptools 42.0.2
six 1.14.0
smmap2 2.0.5
stevedore 1.32.0
toml 0.10.0
typed-ast 1.4.1
virtualenv 16.6.1
virtualenv-clone 0.5.3
wcwidth 0.1.8
wheel 0.33.6
wrapt 1.11.2
zipp 2.2.0
python -m pytest
===== test session starts =====
platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /home/travis/build/kthy/uml.cs, inifile: pytest.ini
plugins: cov-2.8.1
collected 41 items
为了便于比较,这里有 table 个差异:
| Local (WSL) | Travis |
| ------------------ | ---------------------- |
| Ubuntu 18.04.4 LTS | Ubuntu 18.04.3 LTS |
| | atomicwrites-1.3.0 |
| | certifi-2019.6.16 |
| | mock-3.0.5 |
| | nose-1.3.7 |
| | numpy-1.18.0 |
| pip-20.0.2 | pip-19.3.1 |
| | pipenv-2018.11.26 |
| setuptools-45.1.0 | setuptools-42.0.2 |
| | virtualenv-16.6.1 |
| | virtualenv-clone-0.5.3 |
| wheel-0.34.2 | wheel-0.33.6 |
可以看到来自 Travis 的完整构建日志,例如here.
所以, 问题出在我的导入上。无论出于何种原因,我都在导入 Enum
之类的
try:
from features import Access
except (ImportError, ModuleNotFoundError):
from umldotcs.features import Access
- 我不记得为什么,但可能是因为我之前在导入时遇到过一些问题。 Cargo cult 编程再次来袭! 运行 pytest
本地使用了第二个导入,而 Travis 出于某种原因选择了第一个,触发 Issue30545 这是用户错误,不是 python 错误。确保所有 Enum
都以相同的方式导入解决了问题。
晕!我在检查枚举的相等性时遇到奇怪的问题。我的本地测试通过了,但在 Travis 上却失败了。这是 Travis 的测试失败示例:
_____________________________ test_extract_object ______________________________
def test_extract_object():
"""Test UmlCreator.extract_object()."""
creator = UmlCreator(".")
with pytest.raises(ValueError):
creator.extract_object(["no", "klass", "included"])
obj = creator.extract_object(
[
"public",
"static",
"class",
"StaticKlass",
":",
"ICanBeImplemented,",
"IComparable,",
"IEquatable<Klass>",
]
)
assert obj.__class__.__name__ == "UmlClass"
> assert obj.access is Access.PUBLIC
E assert Access("public") is Access("public")
E + where Access("public") = UmlClass(['StaticKlass', ':', 'ICanBeImplemented,', 'IComparable,', 'IEquatable<Klass>'], **{'nsp': None, 'access': Access("public"), 'attrs': [], 'modifiers': [Modifier("static")], 'repo_url': None}).access
E + and Access("public") = Access.PUBLIC
tests/test_creator.py:44: AssertionError
如果我将有问题的行更改为 assert obj.access.value == Access.PUBLIC.value
,它也会传递给 Travis,但我很困惑为什么它在一个地方失败并通过基本相同的设置在另一个地方传递,并且它的当然也会影响 non-test 代码,这意味着我在代码中使用 foo is Enum.VAL
的任何地方,对这些方法的测试也会失败:
tests/test_creator.py:44: AssertionError
_________________________ test_uml_class_display_name __________________________
def test_uml_class_display_name():
"""Test UmlClass.display_name()."""
klass = UmlClass(["Classy"])
assert klass.display_name() == "Classy"
abstract_klass = UmlClass(["Classy"], modifiers=[Modifier.ABSTRACT])
> assert abstract_klass.is_abstract()
E assert False
本地
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
$ pip list
Package Version
------------------ --------
appdirs 1.4.3
astroid 2.3.3
attrs 19.3.0
bandit 1.6.2
black 19.10b0
Click 7.0
coverage 5.0.3
gitdb2 2.0.6
GitPython 3.0.7
importlib-metadata 1.5.0
isort 4.3.21
lazy-object-proxy 1.4.3
mccabe 0.6.1
more-itertools 8.2.0
packaging 20.1
pathspec 0.7.0
pbr 5.4.4
pip 20.0.2
pluggy 0.13.1
py 1.8.1
pylint 2.4.4
pyparsing 2.4.6
pytest 5.3.5
pytest-cov 2.8.1
PyYAML 5.3
regex 2020.1.8
rope 0.16.0
setuptools 45.1.0
six 1.14.0
smmap2 2.0.5
stevedore 1.32.0
toml 0.10.0
typed-ast 1.4.1
wcwidth 0.1.8
wheel 0.34.2
wrapt 1.11.2
zipp 2.2.0
$ pipenv run test
===== test session starts =====
platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /mnt/c/Users/thy/source/repos/uml.cs, inifile: pytest.ini
plugins: cov-2.8.1
collected 41 items
特拉维斯
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic
$ pip list
Package Version
------------------ ----------
appdirs 1.4.3
astroid 2.3.3
atomicwrites 1.3.0 !
attrs 19.3.0
bandit 1.6.2
black 19.10b0
certifi 2019.6.16 !
Click 7.0
coverage 5.0.3
gitdb2 2.0.6
GitPython 3.0.7
importlib-metadata 1.5.0
isort 4.3.21
lazy-object-proxy 1.4.3
mccabe 0.6.1
mock 3.0.5 !
more-itertools 8.2.0
nose 1.3.7 !
numpy 1.18.0 !
packaging 20.1
pathspec 0.7.0
pbr 5.4.4
pip 19.3.1
pipenv 2018.11.26
pluggy 0.13.1
py 1.8.1
pylint 2.4.4
pyparsing 2.4.6
pytest 5.3.5
pytest-cov 2.8.1
PyYAML 5.3
regex 2020.1.8
rope 0.16.0
setuptools 42.0.2
six 1.14.0
smmap2 2.0.5
stevedore 1.32.0
toml 0.10.0
typed-ast 1.4.1
virtualenv 16.6.1
virtualenv-clone 0.5.3
wcwidth 0.1.8
wheel 0.33.6
wrapt 1.11.2
zipp 2.2.0
python -m pytest
===== test session starts =====
platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /home/travis/build/kthy/uml.cs, inifile: pytest.ini
plugins: cov-2.8.1
collected 41 items
为了便于比较,这里有 table 个差异:
| Local (WSL) | Travis | | ------------------ | ---------------------- | | Ubuntu 18.04.4 LTS | Ubuntu 18.04.3 LTS | | | atomicwrites-1.3.0 | | | certifi-2019.6.16 | | | mock-3.0.5 | | | nose-1.3.7 | | | numpy-1.18.0 | | pip-20.0.2 | pip-19.3.1 | | | pipenv-2018.11.26 | | setuptools-45.1.0 | setuptools-42.0.2 | | | virtualenv-16.6.1 | | | virtualenv-clone-0.5.3 | | wheel-0.34.2 | wheel-0.33.6 |
可以看到来自 Travis 的完整构建日志,例如here.
所以,Enum
之类的
try:
from features import Access
except (ImportError, ModuleNotFoundError):
from umldotcs.features import Access
- 我不记得为什么,但可能是因为我之前在导入时遇到过一些问题。 Cargo cult 编程再次来袭! 运行 pytest
本地使用了第二个导入,而 Travis 出于某种原因选择了第一个,触发 Issue30545 这是用户错误,不是 python 错误。确保所有 Enum
都以相同的方式导入解决了问题。