相关疑难解决方法(0)

从py.test fixture返回多个对象

所以我正在学习如何通过测试简单的事件发射器实现来使用py.test.基本上,它看起来像这样

class EventEmitter():
    def __init__(self):
        ...
    def subscribe(self, event_map):
        # adds listeners to provided in event_map events
    def emit(self, event, *args):
        # emits event with given args
Run Code Online (Sandbox Code Playgroud)

为方便起见,我创建了在测试中使用的Listener类

class Listener():
    def __init__(self):
        ...
    def operation(self):
        # actual listener
Run Code Online (Sandbox Code Playgroud)

目前测试看起来如下

@pytest.fixture
def event():
    ee = EventEmitter()
    lstr = Listener()
    ee.subscribe({"event" : [lstr.operation]})
    return lstr, ee

def test_emitter(event):
    lstr = event[0]
    ee = event[1]
    ee.emit("event")
    assert lstr.result == 7 # for example
Run Code Online (Sandbox Code Playgroud)

为了测试事件发射器,我需要检查事件传播后监听器的内部状态是否已经改变.因此,我需要两个对象,我不知道是否有更好的方法来做到这一点(也许用两场比赛,而不是一个以某种方式),因为这看起来有点丑我

python unit-testing pytest python-3.x

13
推荐指数
3
解决办法
4532
查看次数

标签 统计

pytest ×1

python ×1

python-3.x ×1

unit-testing ×1