Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unittest call confusion

I have written a unittest of a program MachineSettings_test.py of mine of the following form:

import unittest
import MachineSettings as MS

class TestMachineSettings(unittest.TestCase):
    def setUp(self):
        [...]

    def testStringRepresentation(self):
        [...]

    def testCasDict(self):
        [...]

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

I am a little bit confused by the following fact: If I run

python -m unittest -v MachineSettings_test

I get as output

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

i.e. Python does not recognize the tests inside the unittesting module.

But if I just run

python MachineSettings_test.py

Everything works fine and I get as output

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

This is confusing to me and I could not find any similar question here yet, so I posted it.

The version of Python that I am (forced to be) using is 2.6, but I could not find anything in the documentation that makes this case to be special.

Anyone an idea?

Thanks

like image 354
Albert Avatar asked May 26 '26 12:05

Albert


1 Answers

From the documentation:

Changed in version 2.7: In earlier versions it was only possible to run individual test methods and not modules or classes.

And you're trying to run tests for whole modules with python 2.6.

Apparently you can't even run from individual test methods with -m unittest in python 2.6. See this question for details.

You might wanna try nose or nose2.

like image 156
gatto Avatar answered Jun 01 '26 06:06

gatto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!