【问题标题】:pytest: `foo is Enum.FOO` tests True locally, False on travis-cipytest:`foo is Enum.FOO` 在本地测试 True,在 travis-ci 上测试 False
【发布时间】:2020-02-13 15:37:48
【问题描述】:

停下!我在检查 Enums 的相等性时遇到了奇怪的问题。在本地,我的测试通过了,但在 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,但我很难理解为什么这会在一个地方失败并以基本相同的设置传递到另一个地方,当然它也会影响非-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

为了便于比较,以下是差异表:

|本地 (WSL) |特拉维斯 | | ------------------ | ---------------------- | | Ubuntu 18.04.4 LTS | Ubuntu 18.04.3 LTS | | | atomicwrites-1.3.0 | | |证书2019.6.16 | | |模拟3.0.5 | | |鼻子1.3.7 | | | numpy-1.18.0 | |点子20.0.2 |点子19.3.1 | | | pipenv-2018.11.26 | |安装工具-45.1.0 |安装工具-42.0.2 | | |虚拟环境-16.6.1 | | |虚拟环境克隆-0.5.3 | |轮-0.34.2 |轮0.33.6 |

可以看到来自 Travis 的完整构建日志,例如here.

【问题讨论】:

  • 枚举是在哪里以及如何创建的?一个可能的罪魁祸首是定义了 Enum 的模块以不同的名称多次导入。
  • @MrBeanBremen:Enum 的卖点之一是它们是独一无二的,可以通过对象身份进行比较。如果有以不同名称导入的文件(这是显示此特定错误所必需的),那么这就是 kthy 需要修复的问题。
  • 好的,谢谢 - 学习了something new。我会删除我的帖子以免混淆任何人。
  • 很高兴引起你的注意,@EthanFurman - 在发布这个问题之前,我已经找到了Issue30545,但我认为它不适用,因为我的测试在本地工作。代码位于githubtests/test_creator.py 中的失败测试我在from umldotcs.features import Access 中进行测试,umldotcs/creator.py 中的测试代码在其中我有一个 try/except 导入语句:try: \ from features import Access \ except (ImportError, ModuleNotFoundError): \ from umldotcs.features import Access
  • @kthy:我不知道。你可以提出一个新问题。 :)

标签: python enums pytest travis-ci pipenv


【解决方案1】:

所以,as pointed out by Ethan 问题是我的导入。无论出于何种原因,我都在导入 Enums 之类的

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2015-07-28
    • 2020-07-27
    相关资源
    最近更新 更多