Python Forum

Full Version: Unit Testing Set Up and Use
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello again,

I found the unittest documentation and put together this simple scenario based on a tutorial I'm working through.

My question: Is it possible to test a single case instead of both?

Ie.. if I wanted to choose just to run the test_True case. I see there are test skip options in the documentation, I couldn't find anything to test only a specific case.

import unittest

def isDivisible(x, y):
    #is x evenly divisible by y?
    return x % y == 0

class Bool_Unit_Testing(unittest.TestCase):
    def test_True(self):
        self.assertEqual(isDivisible(2,2),True)
    def test_False(self):
        self.assertEqual(isDivisible(2,3),False)
    
if __name__ == "__main__":
    unittest.main()
(Jan-08-2024, 05:17 PM)RockBlok Wrote: [ -> ]Hello again,

I found the unittest documentation and put together this simple scenario based on a tutorial I'm working through.

My question: Is it possible to test a single case instead of both?

Ie.. if I wanted to choose just to run the test_True case. I see there are test skip options in the documentation, I couldn't find anything to test only a specific case.

import unittest

def isDivisible(x, y):
    #is x evenly divisible by y?
    return x % y == 0

class Bool_Unit_Testing(unittest.TestCase):
    def test_True(self):
        self.assertEqual(isDivisible(2,2),True)
    def test_False(self):
        self.assertEqual(isDivisible(2,3),False)
    
if __name__ == "__main__":
    unittest.main()
Have you tried command line interface from documentation? https://docs.python.org/3/library/unitte...-interface
python -m unittest file_name.class_name.function_name
essentially how you call it.
Testing is easier when you have an IDE with a test manager. You should also look at pytest. I've used both and like pytest more.