简体   繁体   English

pytest 是一个类型的实例

[英]Pytest isinstance of a type

I'm pretty new to Python and I'm setting up a little game and I want to test it.我对 Python 很陌生,我正在设置一个小游戏,我想对其进行测试。 Currently, I'm generating an array of objects (Rock, Paper, Scissors) and each of them inherit from a Roll object:目前,我正在生成一个对象数组(Rock、Paper、Scissors),每个对象都继承自一个 Roll 对象:

def build_the_three_rolls():
  return [Rock(), Paper(), Scissors()]

This is my test with py.test:这是我对 py.test 的测试:

def test_building_rolls():
  assert len(build_the_three_rolls()) == 3
  assert isinstance(build_the_three_rolls()[0], Rock)
  assert isinstance(build_the_three_rolls()[1], Paper)
  assert isinstance(build_the_three_rolls()[2], Scissors)

but when I run it, I'm getting the following error:但是当我运行它时,我收到以下错误:

>       assert isinstance(build_the_three_rolls()[1], Paper)
E       assert False
E        +  where False = isinstance(<roll.Paper object at 0x110ab42e8>, Paper)

I don't understand why it fails我不明白为什么失败

Thanks!谢谢!

UPDATE:更新:

Here's the definition of Roll and its child classes:下面是 Roll 及其子类的定义:

class Roll:
def __init__(self, name, defeated_by_self, defeat_self):
    self.name = name
    self.defeated_by_self = defeated_by_self
    self.defeat_self = defeat_self


class Rock(Roll):
    def __init__(self):
        defeated_by_self = {}
        defeated_by_self["Scissors"] = "Scissors"
        defeat_self = {}
        defeat_self["Paper"] = "Paper"
        super().__init__("Rock", defeated_by_self, defeat_self)


class Paper(Roll):
    def __init__(self):
        defeated_by_self = {}
        defeated_by_self["Rock"] = "Rock"
        defeat_self = {}
        defeat_self["Scissors"] = "Scissors"
        super().__init__("Paper", defeated_by_self, defeat_self)


class Scissors(Roll):
    def __init__(self):
        defeated_by_self = {}
        defeated_by_self["Paper"] = "Paper"
        defeat_self = {}
        defeat_self["Rock"] = "Rock"
        super().__init__("Scissors", defeated_by_self, defeat_self)

I hit a similar issue and ultimately discovered that in my case it had to do with a relative import path issue.我遇到了类似的问题,最终发现在我的情况下它与相对导入路径问题有关。 When I investigated MySubClass.__class__ from ipython run in the directory with MyClass's module it was different than the same thing when pytest was run on the tests folder.当我从带有 MyClass 模块的目录中运行的MySubClass.__class__调查MySubClass.__class__ ,它与在测试文件夹上运行 pytest 时的同一件事不同。 Example below:下面的例子:

Directory structure:目录结构:

└── project
    ├── __init__.py
    ├── src
    │   ├── __init__.py
    │   ├── my_module.py
    └── tests
        ├── __init__.py
        └── test_my_module.py

my_module.py: my_module.py:

class MyClass:
    def __init__(self, param1):
        self.param1 = param1

class MySubClass(MyClass):
    def __init__(self, param1):
        super().__init__(param1)

test_my_module.py: test_my_module.py:

def test_my_sub_class():
    pkl_instance = # loaded from a pkl file saved from ipython
    test_instance = MySubClass(param1="This was instantiated in 
    test_my_module.py")

    # assert isinstance(pkl_instance, test_instance) fails
    print(pkl_instance.__class__)   # <class 'my_module.MySubClass'>
    print(test_instance.__class__)  # <class 'project.src.my_module.MySubClass'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM