Testing a method without instantiating class

huangapple go评论60阅读模式
英文:

Testing a method without instantiating class

问题

I followed this approach here, but the issue is that this requires me to duplicate code.

我按照这里的方法,但问题是这需要我复制代码。

I can override the class in my test, but then I also have to copy and paste the method from my original script to the test script which defeats the purpose of unit testing. Someone could change the original method, but not the one used in the unit test.

我可以在我的测试中覆盖该类,但这样我还必须将方法从原始脚本复制粘贴到测试脚本中,这违背了单元测试的目的。有人可以更改原始方法,但不能更改单元测试中使用的方法。

main.py

class ClassToTest(object):
    def __init__(x, y, z, many_dependencies):
        self.x = x
        self.y = y
        self.z = z
        self.many_dependencies = many_dependencies

    def method_to_test(self, y):
        self.x = y
        return 5

test_main.py

from main import ClassToTest 

class ClassToTest(object):
    # I want to override this only
    def __init__(self):
        pass

    # I don't want to copy and paste this into my test script
    def method_to_test(self, y):
        self.x = y
        return 5

fake_instance = mock.Mock()
ClassToTest.__init__(fake_instance)
x = ClassToTest.method_to_test(fake_instance, 3)
assert x == 5
assert fake_instance.x == 3

Is there a way to import the class, override the init method and test the original method without pasting it into the test itself?

是否有一种方法可以导入该类,覆盖init方法并测试原始方法而不必将其粘贴到测试中?

英文:

I followed this approach here, but the issue is that this requires me to duplicate code.

I can override the class in my test, but then I also have to copy and paste the method from my original script to the test script which defeats the purpose of unit testing. Someone could change the original method, but not the one used in the unit test.

main.py

class ClassToTest(object):
    def __init__(x, y, z, many_dependencies):
        self.x = x
        self.y = y
        self.z = z
        self.many_dependencies = many_dependencies

    def method_to_test(self, y):
        self.x = y
        return 5

test_main.py

from main import ClassToTest 

class ClassToTest(object):
    # I want to override this only
    def __init__(self):
        pass

    # I don't want to copy and paste this into my test script
    def method_to_test(self, y):
        self.x = y
        return 5

fake_instance = mock.Mock()
ClassToTest.__init__(fake_instance)
x = ClassToTest.method_to_test(fake_instance, 3)
assert x == 5
assert fake_instance.x == 3

Is there a way to import the class, override the init method and test the original method without pasting it into the test itself?

答案1

得分: 0

从该类继承并重写初始化方法?

from main import ClassToTest

class ClassToTest2(ClassToTest):
    def __init__(self):
        pass

您将没有x属性,但您共享的方法仍然可用。

英文:

Maybe inherit from the class and override the init?

from main import ClassToTest

class ClassToTest2(ClassToTest):
    def __init__(self):
        pass

You won't have the x attribute but the method you shared should still work

huangapple
  • 本文由 发表于 2023年4月13日 19:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定