How to perform a unittest on a unittest - python

How do you do a unittest on a unittest. I have a unittest and obviously when it is succesfully it generates an ok message in the terminal but I have struggled to find a way of creating a test for the test based upon that.
Basically looking for a way to test if the below test returns ok.
The Test Below:
import unittest
class TestSum(unittest.TestCase):
def test_sum(self):
self.assertEqual(2+2, 4)
if __name__ == '__main__':
unittest.main()
What this returns in the shell :
Ran 1 test in 0.000s
OK

First of all you have to have function that does the calculation, so you pass that function to the test case.
Example:
import unittest
# Defined the function that does the calculation
def sum_numbers(x, y):
return x + y
class TestSum(unittest.TestCase):
def test_sum(self):
self.assertEqual(sum_numbers(2, 2), 4)
if __name__ == '__main__':
unittest.main()
Now this test will evaluate to ok and you can add more test to test_sum

Related

Creating Unittest class inside function not working

In the attached script why 0 testcases are running
import unittest
def smg():
def add(x, y):
return x + y
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEquals(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
smg()
Gives
Ran 0 tests in 0.000s
What can be done to fix it kindly assist
You might be interested with unittest.TextTestRunner:
A basic test runner implementation that outputs results to a stream.
Sample usage:
However, should you want to customize the building of your test suite, you can do it yourself:
def suite():
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase('test_default_widget_size'))
suite.addTest(WidgetTestCase('test_widget_resize'))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite())
Sample run for your case.
src.py
def add(x, y):
print("Add", x, y)
return x + y
test_src.py
import unittest
from src import add
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEqual(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
Running tests is as how it's done normally
$ python test_src.py # Using unittest
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$ pytest -q # Using pytest
.
1 passed in 0.06s
Now if you want to manually call it via function.
run_tests.py
import unittest
import test_src
def suite():
suite = unittest.TestSuite()
suite.addTest(test_src.SimpleTest('testadd1'))
return suite
def run():
runner = unittest.TextTestRunner()
runner.run(suite())
# run() # Uncomment if you want to try to run it as a script e.g. <python run_tests.py>
You can now just import the file and call run() whenever you need:
$ python3
>>> import run_tests
>>> run_tests.run()
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
>>>
Take out the code inside of smg function and then run tests from your command-line with python -m unittest <Your Filename>.py.
Your code will look like this:
import unittest
def add(x, y):
return x + y
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEquals(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
Also you may get a deprecation warning for assertEquals. You might want to change it to assertEqual instead.

Python doesn't detect my unittest

I have following code snippet -
import unittest
class SimpleWidgetTestCase(unittest.TestCase):
def setUp(self):
print 'setup'
def method_test(self):
print 'test method'
def tearDown(self):
print 'tear down'
if __name__ == "__main__":
unittest.main()
Output -
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
test method name should start with test. Replace method_test to test_method, and try again.
From unittest documentation:
A testcase is created by subclassing unittest.TestCase. The three
individual tests are defined with methods whose names start with the
letters test. This naming convention informs the test runner about
which methods represent tests.

PyUnit: How to run all tests from different unittest.TestCase subclass present in a file

import unittest
import HTMLTestRunner
class TestClass1(unittest.TestCase):
def setUp(self):
pass
def case1(self):
assert 4 == 3
def case2(self):
assert 4 == 4
def tearDown(self):
pass
class TestClass2(unittest.TestCase):
def setUp(self):
pass
def case3(self):
assert 1 == 2
def tearDown(self):
pass
def suite():
suite = unittest.TestSuite()
suite.addTest(TestClass1(['case1','case2']))
suite.addTest(TestClass2('case4'))
return suite
test_suite = suite()
unittest.TextTestRunner(verbosity=2).run(test_suite)
fp = file('my_report.html', 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='My unit test',
description='This demonstrates the report output by HTMLTestRunner.'
)
runner.run(test_suite)
I am trying to run all the methods in both the classes in a single run. However, the code above did not do so. In the suite function, I tried to add multiple tests from the classes but that also did not work and was giving an error.
From this answer at the question "Is test suite deprecated in PyUnit?":
"unittest.TestSuite is not necessary if you want to run all the tests in a single module as unittest.main() will dynamically examine the module it is called from and find all classes that derive from unittest.TestCase."
There's more in that answer about when unittest.TestSuite is useful.
That said, I needed to make some changes to get these tests to work. Firstly, unittest looks for functions with "test_" at their start. Also, unittest's assertEqual and similar methods should be used, instead of just Python's assert statement. Doing that and eliminating some unneeded code led to:
import unittest
class TestClass1(unittest.TestCase):
def test_case1(self):
self.assertEqual(4, 3)
def test_case2(self):
self.assertEqual(4, 4)
class TestClass2(unittest.TestCase):
def test_case3(self):
self.assertEqual(1, 2)
unittest.main()
This produced appropriate output (3 tests run with 2 failures), which I won't reproduce here in the interest of space.

Python suite test doesn't run tests

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?
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)
modify your setUp method as follows
def setUp(self):
self.os = OS()
pass

str object is not callable for unittest

I have written a test case which shows the error
from unittest import *
class MyTest(unittest.TestCase):
def test_add(self):
self.assertEquals(1,(2-1),"Sample Subraction Test")
if __name__ == '__main__':
unittest.main()
Output:
Str object is not callable
Instead of
"from unittest import *" I have given
"import unittest"
it worked
but still i couldn't get point it accurately
what might be the reason for this?
from ... import * is dangerous practice, and should only be used when the module/package has been designed and advertised that way, and you have a good reason to do so.
It turns out that unittest has not been designed that way, and when that method is used two other 'test cases' are found, but since they aren't really test cases, they create problems.
The correct way to do what you want is:
import unittest
class MyTest(unittest.TestCase):
def test_subtraction(self):
self.assertEqual(1, (2-1), "Sample Subraction Test")
if __name__ == '__main__':
unittest.main()
In researching this issue I discovered that the __all__ variable can and should be used to define the public API -- its presence does not indicate that from ... import * is supported.
I got it working like this.
Override runTest() method, create instance, run your test_add()
from unittest import TestCase
class MyTest(TestCase):
def runTest(self):
pass
def test_add(self):
self.assertEquals(1,(2-2),"Sample Subraction Test")
if __name__ == '__main__':
test = MyTest()
test.test_add()

Categories