为什么mocking在nosetests中存在代码多次测试后仍然有效?

发布于 2024-12-21 05:44:19 字数 788 浏览 2 评论 0原文

我有以下代码 test_A.py ,它模拟 MyClass.mymethod:

from unittest import main
from mocker import Mocker, MockerTestCase
Class test_A(MockerTestCase):
  def setUp(self):
    self.m=Mock()
    MyClass.mymethod = self.m.mock()
    self.m.result(None)
    self.m.count(0,None)
    self.m.replay()

  def test_me(self):
    #Do something about MyClass.method

  def tearDown(self):
    self.m.restore()
    self.m.verify()

我还有另一个代码 test_B.py ,它不模拟 MyClass.mymethod:

Class test_B(MockerTestCase):
  def setUp(self):
    pass

  def test_me(self):
    #Do something about MyClass.method

  def tearDown(self):
     pass

但是,当我执行“nosetests test_A.py test_B.py”时,测试后看起来像test_A.py 并进入 test_B.py,MyClass.mymethod 仍然是模拟的。不知道为什么以及如何解决它。谢谢!

I have the following code test_A.py which mocks MyClass.mymethod:

from unittest import main
from mocker import Mocker, MockerTestCase
Class test_A(MockerTestCase):
  def setUp(self):
    self.m=Mock()
    MyClass.mymethod = self.m.mock()
    self.m.result(None)
    self.m.count(0,None)
    self.m.replay()

  def test_me(self):
    #Do something about MyClass.method

  def tearDown(self):
    self.m.restore()
    self.m.verify()

I also have another code test_B.py which DOES NOT mock MyClass.mymethod:

Class test_B(MockerTestCase):
  def setUp(self):
    pass

  def test_me(self):
    #Do something about MyClass.method

  def tearDown(self):
     pass

However, when I do "nosetests test_A.py test_B.py", it looks like after testing test_A.py and entering test_B.py, MyClass.mymethod is still mocked up. Not sure why and how to get around it. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-12-28 05:44:19

该行:

MyClass.mymethod = self.m.mock()

确实用新对象替换了 MyClass.mymethod() 。对 MyClass.mymethod 的所有后续引用都将指向模拟对象,即使这些引用位于不同的类中。

您想要的是一种替换仅在类 test_A 中工作的 MyClass.mymethod() 的方法。实现此目的的最简单方法是在 tearDown 方法中恢复原始的 mymethod

Class test_A():
    def setUp(self):
        self.originalMyMethod = MyClass.mymethod
        self.m=Mock()
        MyClass.mymethod = self.m.mock()
        self.m.result(None)
        self.m.count(0,None)
        self.m.replay()

    def test_me(self):
        # Do something about MyClass.mymethod

    def tearDown(self):
        self.m.restore()
        self.m.verify()
        MyClass.mymethod = self.originalMyMethod

The line:

MyClass.mymethod = self.m.mock()

really does replace MyClass.mymethod() with a new object. All subsequent references to MyClass.mymethod will be to the mock object, even if those references are in a different class.

What you want is a way to replace MyClass.mymethod() that only works in class test_A. The simplest way to achieve this would be to restore the original mymethod in your tearDown method:

Class test_A():
    def setUp(self):
        self.originalMyMethod = MyClass.mymethod
        self.m=Mock()
        MyClass.mymethod = self.m.mock()
        self.m.result(None)
        self.m.count(0,None)
        self.m.replay()

    def test_me(self):
        # Do something about MyClass.mymethod

    def tearDown(self):
        self.m.restore()
        self.m.verify()
        MyClass.mymethod = self.originalMyMethod
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文