简体   繁体   中英

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. I'm familiar with the assertListEquals(a,b) but I may be using it incorrectly or im missing something. 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

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.

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 __name__ == "main":

to

if __name__ == "__main__":

as that is the correct naming convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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