pytest:`foo是Enum.foo`在本地测试True,在travisci上测试False

2024-09-27 21:24:07 发布

您现在位置:Python中文网/ 问答频道 /正文

停!我在检查枚举的相等性时遇到了奇怪的问题。在本地我的测试通过了,但在特拉维斯他们失败了。以下是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


Tags: pipnamepytesttravisobjectaccesspytest
1条回答
网友
1楼 · 发布于 2024-09-27 21:24:07

所以,问题是我的进口。不管出于什么原因,我导入了Enum

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

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

相关问题 更多 >

    热门问题