gpt4 book ai didi

python - pytest: `foo is Enum.FOO` 在本地测试为 True,在 travis-ci 上测试为 False

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:49 25 4
gpt4 key购买 nike

住手!我在检查枚举的相等性时遇到了奇怪的问题。在本地,我的测试通过了,但在特拉维斯上它们失败了。以下是 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,但我很困惑为什么会在一个地方失败并传递另一个具有基本相同设置的方法,它当然也会影响非测试代码,这意味着我在代码中使用 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

为了便于比较,这里有一个差异表:

| 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 .

最佳答案

所以,as pointed out by Ethan问题是我的进口。不管出于什么原因,我导入了Enums,例如

try:
from features import Access
except (ImportError, ModuleNotFoundError):
from umldotcs.features import Access

- 我不记得为什么,但可能是因为我之前在导入时遇到过一些问题。 cargo 邪教编程再次来袭! 😉 在本地运行 pytest 使用了第二个导入,而 Travis 由于某种原因选择了第一个,触发 Issue30545这是用户错误,而不是 python bug。确保以相同的方式导入所有 Enum 解决了问题。

关于python - pytest: `foo is Enum.FOO` 在本地测试为 True,在 travis-ci 上测试为 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60211486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com