简体   繁体   English

为单元测试正确创建对象实例

[英]Properly creating object instances for unit test

I'm trying to run a unit test in a separate unit test file located in the same directory, but running the unit test file just runs the functionality within the main file, while not calling upon the instance I've tried to create.我试图在位于同一目录中的单独单元测试文件中运行单元测试,但运行单元测试文件只是运行主文件中的功能,而不是调用我尝试创建的实例。 Here's the code for the main file:这是主文件的代码:

class ListOperations:
    def list_copy(l):
        return [x for x in l]

    print(list_copy(["What","A", "List"]))

Here's the unit test code located in the same directory:这是位于同一目录中的单元测试代码:

import unittest
from basicfunction import ListOperations

class TestListOperations(unittest.TestCase):

    def test_1(self):
        ListOperations = ListOperations()
        self.assertEqual(ListOperations.list_copy(["This", "Is", "Just", "A", "List"]), ["This", "Is", "Just", "A", "List"])

    def test_2(self):
        self.assertTrue(True)

if __name__ == '__main__':
    unittest.main()

When I try to run the unit test, I get the output:当我尝试运行单元测试时,我得到输出:

E.
======================================================================
ERROR: test_1 (__main__.TestListOperations)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/path/basicfunction.ut.py", line 8, in test_1
    ListOperations = ListOperations()
UnboundLocalError: local variable 'ListOperations' referenced before assignment

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (errors=1)
['What', 'A', 'List']

How could I set this test up properly?我怎样才能正确设置这个测试?

Edit: @staticmethod helped to run the test above, but I'm having issues with methods with two parameters.编辑: @staticmethod有助于运行上面的测试,但我在使用带有两个参数的方法时遇到了问题。 If I try to create an instance outside of the unittest class:如果我尝试在 unittest 类之外创建一个实例:

lop = ListOperations()
intersect1 = ["Pikachu", "Evolves", "Into", "Raichu"]
intersect2 = ["Raichu", "Does", "Not", "Evolve", "Into", "Anything"]
print(lop.list_intersect(intersect1, intersect2))

Output is ['Into', 'Raichu'] in the main file, but I get an error of TypeError: list_intersect() takes 2 positional arguments but 3 were given when I try to import into the test file.主文件中的输出是['Into', 'Raichu'] ,但我收到TypeError: list_intersect() takes 2 positional arguments but 3 were given错误TypeError: list_intersect() takes 2 positional arguments but 3 were given在我尝试导入测试文件时TypeError: list_intersect() takes 2 positional arguments but 3 were given Any thoughts on how I could resolve this error?关于如何解决此错误的任何想法?

Firstly, in OOP for Python, when you define a method you will need to put the parameter self in the first position.首先,在面向 Python 的 OOP 中,当您定义一个方法时,您需要将参数self放在第一位。 This is to give the method access to the underlying object that it is tied to.这是为了让方法访问它所绑定的底层对象。

class ListOperations:
    def list_copy(self, l):
        return [x for x in l]

The other issue causing the error message is your variable name ListOperations .导致错误消息的另一个问题是您的变量名称ListOperations That word is taken by your class ListOpertions so you can change it to listOperations so that they don't collide.该词由您的类ListOpertions因此您可以将其更改为listOperations以便它们不会发生冲突。

import unittest
from basicfunction import ListOperations

class TestListOperations(unittest.TestCase):

    def test_1(self):
        listOperations = ListOperations()
        self.assertEqual(listOperations.list_copy(["This", "Is", "Just", "A", "List"]), ["This", "Is", "Just", "A", "List"])

    def test_2(self):
        self.assertTrue(True)

if __name__ == '__main__':
    unittest.main()

I think there is small bug in your code you are using same name for variable as the name of your class ListOperations .我认为您的代码中有一个小错误,您使用的变量名称与类ListOperations的名称相同

Try changing variable name to something else and run the code尝试将变量名称更改为其他名称并运行代码

For example例如

lop = ListOperations() 
self.assertEqual(lop.list_copy(["This", "Is", "Just", "A", "List"]), ["This", "Is", "Just", "A", "List"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM