使用两次的Pytest治具会不会分配自己的内存?

问题描述

我正在编写一个程序来自动执行cisco命令行脚本。在pytest中,我制作了一个返回路由器的夹具。该类会跟踪实例,以确保路由器不具有相同的名称,如果相同,则抛出ValuError。我想确保确实如此,并使用固定装置制作routerrouter1,使其等于固定装置。我将两者的id()都指向了相同的存储位置。这是我的班级设计是否有问题,还是我不了解pytest和/或python对内存的处理方式?

class Router(Metaclass=IterRouter):
    _all_routers = []
    available_protocols = ['RIP1','RIP2','OSPF1','OSPF2']

    def __init__(self,hostname):
        # If the hostname already exists,then raise a ValueError
        double_list = [hostname == router.hostname for router in self._all_routers]
        if sum(double_list) != 0:
            raise ValueError(f"Router hostname <{hostname}> already exists")
        # Add new router to list of instances.
        Router._all_routers.append(self)
        # Set class instance attributes.
        self.hostname = hostname

@pytest.fixture(scope='module')
def default_router():
    name = 'default'
    return Router(name)

def test_invalid_args(self,default_router):
"""Test that if you try and make a second Router with the same name,it will fail."""
   router = default_router
   print(id(router))
   print(router.hostname)
   with pytest.raises(ValueError):
      router1 = default_router
      print(id(router1))

结果是,对于routerrouter1而言,我都获得了2383405448304的身份,并且测试失败并提示ValueError未能引发。

当我用下面的代码实例化一个新的路由器来关闭测试时,测试通过了

def test_invalid_args(self,it will fail."""
   router = default_router
   with pytest.raises(ValueError):
      router1 = Router("default")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...