简体   繁体   English

Python单元测试比较列表值

[英]Python unit test comparing list values

So I am currently trying to write a simple test that will just compare the value of one list to another in a separate py file. 因此,我目前正在尝试编写一个简单的测试,该测试仅将一个列表的值与另一个py文件中的另一个列表进行比较。 I'm familiar with the assertListEquals(a,b) but I may be using it incorrectly or im missing something. 我对assertListEquals(a,b)很熟悉,但可能使用不正确或缺少某些内容。 When I run the module I get absolutely no response from the testsuite. 当我运行模块时,测试套件绝对没有任何响应。 No Error codes or anything. 没有错误代码或其他任何内容。 Maybe you guys can help me. 也许你们可以帮我。

Here is my testsuite 这是我的考生

from MyList import main
import unittest

class TestSuite(unittest.TestCase):



    def test_ValueContains(self):

        print('test')

        testList = [1,2,3]
        value = MyList
        self.assertListEqual(testList,value)


if __name__== "main":
            unittest.main()

And here is the MyList.py 这是MyList.py

def main():

    MyList = [1,2,3]

main()

All I want to do is be able to check if MyList == testList and get a 'Ok' when running the test. 我要做的就是能够检查MyList == testList并在运行测试时获得“ OK”。

Sorry I'm new to unit tests 抱歉,我是单元测试的新手

Try modifying your code as such: 尝试这样修改代码:

def main():
    MyList = [1,2,3]
    return MyList

and

from MyList import main
import unittest

class TestSuite(unittest.TestCase):

    def test_ValueContains(self):

        print('test')

        testList = [1,2,3]
        value = main()
        self.assertListEqual(testList,value)

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

The problem was that you were running your main method without ever storing the value. 问题是您在运行主方法时没有存储值。 This can be achieved by returning the value instead of simply assigning it. 这可以通过返回值而不是简单地赋值来实现。

You also needed to change the if statement at the bottom from 您还需要从下面更改底部的if语句

if __name__ == "main":

to

if __name__ == "__main__":

as that is the correct naming convention. 因为这是正确的命名约定。

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

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