我是Python的新手,我正在设置一个小游戏,我想测试它。 目前,我正在生成一个对象数组(Rock,Paper,Scissors),每个对象都继承自Roll对象:
def build_the_three_rolls():
return [Rock(), Paper(), Scissors()]
这是我对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)
但是当我运行它时,我收到以下错误:
> assert isinstance(build_the_three_rolls()[1], Paper)
E assert False
E + where False = isinstance(<roll.Paper object at 0x110ab42e8>, Paper)
我不明白为什么会失败
谢谢!
更新:
这是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)
答案 0 :(得分:0)
我遇到了一个类似的问题,最终发现我的情况与一个相对的导入路径问题有关。当我从MyPython的目录下的ipython中调查onCreate()
时,与在tests文件夹中运行pytest时的情况有所不同。下面的示例:
目录结构:
MySubClass.__class__
my_module.py:
└── project
├── __init__.py
├── src
│ ├── __init__.py
│ ├── my_module.py
└── tests
├── __init__.py
└── test_my_module.py
test_my_module.py:
class MyClass:
def __init__(self, param1):
self.param1 = param1
class MySubClass(MyClass):
def __init__(self, param1):
super().__init__(param1)