1

I am trying to code a suite test, I have one module which runs unit test correctly but I intend to add more modules and test them at once, so I have coded the following code:

#main.py

import unittest
from test.Services import TestOS

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTests( TestOS.TestOS() )
    unittest.TextTestRunner().run(suite)

TestOS.py

import unittest
from app.Services.OS import OS

class TestOS(unittest.TestCase):
    os = OS()
    def setUp(self):
        pass
    def tearDown(self):
        pass
    def testOSName(self):
        self.assertEquals(self.os.getPlatform(), 'Windows')    
    def testOSVersion(self):
        self.assertEquals(self.os.getVersion(), '7')

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

After running it, I get this output:

Finding files... done.
Importing test modules ... done.

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

OK

It didn't find any test, What's wrong with my code?

1
  • 2
    Include the code for your tests in your post. Commented Aug 10, 2012 at 21:31

2 Answers 2

4

suite.addTest( TestOS.TestOS() ) works only if your testcase contains a runTest() function. Otherwise you need a "TestLoader" to detect the functions of TestOS that start with "test*".

#main.py

import unittest
from test.Services import TestOS

if __name__ == '__main__':
    suite = unittest.TestSuite()
    tests = unittest.defaultTestLoader.loadTestsFromTestCase(TestOS)
    suite.addTests(tests)
    unittest.TextTestRunner().run(suite)
Sign up to request clarification or add additional context in comments.

3 Comments

I changed my code but still didnt run the tests, Are you sure it is correct?
I did test it, but I don't have the complete code of course. Maybe you need unittest.defaultTestLoader.loadTestsFromTestCase(TestOS.TestOS) ?
Here you can see it run on Ideone. Do you get any errors? Or does it just not run.
0

modify your setUp method as follows

def setUp(self):
    self.os = OS()
    pass

1 Comment

Why would this make a difference? (The only reason it would make any difference is if multiple TestOSs are instantiated for the same run of the program and the OS actually changes between those instantiations, which sounds incredibly unlikely).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.