Error generated when running unit test 'None' - python

I am new to python and trying to run a unit test. My function works well when using the print method, but while trying to develop a test module I keep getting errors.
my function in the python file(work.py)
def lee(n):
for i in range(1,n+1):
print (i)
my unit test module
import unittest
import work
class TestWork(unittest.TestCase):
def test_lee(self):
result = work.lee(3)
self.assertEqual(result, [1,2,3])
if __name__ == '__main__':
unittest.main()
errors generated
======================================================================
FAIL: test_lee (__main__.TestWork)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\test_work.py", line
12, in test_lee
self.assertEqual(result, [1,2,3])
AssertionError: None != [1, 2, 3]

Here,
def lee(n):
list = []
for i in range(1,n+1):
list.append(i)
return list
As for printing, do print(work.lee(3))

Related

How to run unit tests in python?

I am having a difficult time understanding how to use assertions in unit tests in Python.
Original code:
class A:
def method(file):
if file:
<do something>
else:
raise Exception("file not found")
Now to create its test. Let's say I don't want to pass a file and test it.
t1 = A()
class Test(TestCase):
def test_method_no_path(self):
t1.method(' ') #passed no file
<Now do what> ??
self.assert ??
# tests/test_ex.py
from os import path
from unittest import TestCase
class Error(Exception):
pass
class FileChecker:
def process_file(self, f_path: str):
if path.exists(f_path) and path.isfile(f_path):
# just an example
return 'result'
raise Error(f_path)
class TestFileChecker(TestCase):
_CHECKER = FileChecker()
def test_done(self):
# check method result
self.assertTrue(self._CHECKER.process_file('/tmp/1.txt') == 'result')
def test_error(self):
# check method exception
with self.assertRaises(Error):
self._CHECKER.process_file('/tmp/1.txt')
Run our test(nosetests tests/test_ex.py). test_done failed because /tmp/1.txt file does not exist:
======================================================================
ERROR: test_done (tests.test_ex.TestFileChecker)
Let's create a file(echo 'test' >> /tmp/1.txt) and run test one more time:
======================================================================
FAIL: test_error (tests.test_ex.TestFileChecker)
As you can see test_done works fine, because we got expected result, but now we have problems with test_error(because Error wasn't raised).

How can generate automatically test case number in unittest?

how can I generate automatically the numbers of test cases in unittest? I mean something like test_01, test_02, test_{generate number}.
import unittest
class TestSum(unittest.TestCase):
def test_01_sum(self):
self.assertEqual(sum([1, 2, 3]), 6, "Should be 6")
def test_02_sum_tuple(self):
self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")
if name == '__main__':
unittest.main()
To achieve this at runtime, you can actually rename the test methods for your test class:
def generate_test_numbers(test_class):
counter = 1
for method_name in dir(test_class):
if not method_name.startswith('test_N_'):
continue
method = getattr(test_class, method_name)
if not callable(method):
continue
new_method_name = method_name.replace('_N_', '_{:02d}_'.format(counter))
counter += 1
setattr(test_class, new_method_name, method)
delattr(test_class, method_name)
return test_class
You can either simply call this function from main:
generate_test_numbers(TestSum)
or as #VPfB suggested it, use it as a class decorator:
#generate_test_numbers
class TestSum(unittest.TestCase):
def test_N_sum(self):
self.assertEqual(sum([1, 2, 3]), 6, "Should be 6")
def test_N_sum_tuple(self):
self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")
Either will output (with -v)
test_01_sum (__main__.TestSum) ... ok
test_02_sum_tuple (__main__.TestSum) ... FAIL
======================================================================
FAIL: test_02_sum_tuple (__main__.TestSum)
----------------------------------------------------------------------
Traceback (most recent call last):
File "q.py", line 8, in test_N_sum_tuple
self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")
AssertionError: 5 != 6 : Should be 6
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)

Pass Raised Error in function to Exception Raised by UnitTest [duplicate]

This question already has answers here:
How to write a custom `.assertFoo()` method in Python?
(3 answers)
Closed 7 years ago.
I would like to write a function that raises an error when it fails and then passes that to unittest. Consider the following:
class undefinedTerms(unittest.TestCase):
def setUp(self):
self.A = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4,8))
self.B = frozenset((1,4,5,6,3,4,2,5,4,3,1,3,4,2,5,3,6,7,4,2,3,1))
self.C = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4))
self.D = (1,2,1)
def is_a_set(self,set_this):
try:
assert isinstance(set_this, (set,frozenset))
except TypeError:
raise TypeError("The object you passed is not a set.")
return True
def test_A_is_a_set(self):
self.assertTrue(self.is_a_set(self.A))
def test_B_is_a_set(self):
self.assertTrue(self.is_a_set(self.B))
def test_C_is_a_set(self):
self.assertTrue(self.is_a_set(self.C))
def test_D_is_a_set(self):
self.assertTrue(self.is_a_set(self.D), self.is_a_set(self.D))
suite = loader.loadTestsFromTestCase(undefinedTerms)
unittest.TextTestRunner(verbosity=2).run(suite)
This gives the following output.
test_A_is_a_set (__main__.undefinedTerms) ... ok
test_B_is_a_set (__main__.undefinedTerms) ... ok
test_C_is_a_set (__main__.undefinedTerms) ... ok
test_D_is_a_set (__main__.undefinedTerms) ... FAIL
======================================================================
FAIL: test_D_is_a_set (__main__.undefinedTerms)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-33-4751f8653e7a>", line 27, in test_D_is_a_set
self.assertTrue(self.is_a_set(self.D), self.is_a_set(self.D))
File "<ipython-input-33-4751f8653e7a>", line 12, in is_a_set
(type(set_this) is set))
AssertionError
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
What I would like is for the AssertionError to be the TypeError defined in the function. I am open to radically different implementations.
Update
I think I was unclear as to precisely what I am after. After reading comments and answers I believe what I want is to create a custom assertion.
This has previously been addressed here.
How about using .assertIsInstance()
def assert_is_set(self, set_this):
self.assertIsInstance(set_this, (set, frozenset))
At this point, you don't really need a function and can just inline the check.
You can also use the more general .fail() method if your conditions become more complex.
Signals a test failure unconditionally, with msg or None for the error message
if something:
self.fail("Computer broken!")
The documentation page has a list of all the assertions available in TestCases
As proposed here, I believe what I was after is custom assertion methods. This is the final implementation I went with.
class SetAssertions:
def assertIsASet(self, set_this):
if not isinstance(set_this, (set,frozenset)):
raise AssertionError("The object you passed is not a set.")
import unittest
class undefinedTerms(unittest.TestCase, SetAssertions):
def setUp(self):
self.A = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4,8))
self.B = frozenset((1,4,5,6,3,4,2,5,4,3,1,3,4,2,5,3,6,7,4,2,3,1))
self.C = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4))
self.D = (1,2,1)
def test_A_is_a_set(self):
self.assertIsASet(self.A)
def test_B_is_a_set(self):
self.assertIsASet(self.B)
def test_C_is_a_set(self):
self.assertIsASet(self.C)
def test_D_is_a_set(self):
self.assertIsASet(self.D)
suite = loader.loadTestsFromTestCase(undefinedTerms)
unittest.TextTestRunner(verbosity=2).run(suite)
test_A_is_a_set (__main__.undefinedTerms) ... ok
test_B_is_a_set (__main__.undefinedTerms) ... ok
test_C_is_a_set (__main__.undefinedTerms) ... ok
test_D_is_a_set (__main__.undefinedTerms) ... FAIL
======================================================================
FAIL: test_D_is_a_set (__main__.undefinedTerms)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-39-495718fb2bad>", line 32, in test_D_is_a_set
self.assertIsASet(self.D)
File "<ipython-input-39-495718fb2bad>", line 6, in assertIsASet
raise AssertionError("The object you passed is not a set.")
AssertionError: The object you passed is not a set.
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=1)

Exception ItemNotString not caught with assertRaises

import unittest
import filterList
class TestFilterList(unittest.TestCase):
""" docstring for TestFilterList
"""
def setUp(self):
self._filterby = 'B'
def test_checkListItem(self):
self.flObj = filterList.FilterList(['hello', 'Boy', 1], self._filterby)
self.assertRaises(filterList.ItemNotString, self.flObj.checkListItem)
def test_filterList(self):
self.flObj = filterList.FilterList(['hello', 'Boy'], self._filterby)
self.assertEquals(['Boy'], self.flObj.filterList())
if __name__ == '__main__':
unittest.main()
My above test test_checkListItem() fails , for the below filterList module:
import sys
import ast
class ItemNotString(Exception):
pass
class FilterList(object):
"""docstring for FilterList
"""
def __init__(self, lst, filterby):
super(FilterList, self).__init__()
self.lst = lst
self._filter = filterby
self.checkListItem()
def checkListItem(self):
for index, item in enumerate(self.lst):
if type(item) == str:
continue
else:
raise ItemNotString("%i item '%s' is not of type string" % (index+1, item))
print self.filterList()
return True
def filterList(self):
filteredList = []
for eachItem in self.lst:
if eachItem.startswith(self._filter):
filteredList.append(eachItem)
return filteredList
if __name__ == "__main__":
try:
filterby = sys.argv[2]
except IndexError:
filterby = 'H'
flObj = FilterList(ast.literal_eval(sys.argv[1]), filterby)
#flObj.checkListItem()
Why does the test fail with the error:
======================================================================
ERROR: test_checkListItem (__main__.TestFilterList)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_filterList.py", line 13, in test_checkListItem
self.flObj = filterList.FilterList(['hello', 'Boy', 1], self._filterby)
File "/Users/sanjeevkumar/Development/python/filterList.py", line 16, in __init__
self.checkListItem()
File "/Users/sanjeevkumar/Development/python/filterList.py", line 23, in checkListItem
raise ItemNotString("%i item '%s' is not of type string" % (index+1, item))
ItemNotString: 3 item '1' is not of type string
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=1)
Also, is the approach of the filterList module correct ?
The exception is not being caught by your assertRaises call because it's being raised on the previous line. If you look carefully at the traceback, you'll see that the checkListItem was called by the FilterList class's __init__ method, which in turn was called when you try to create self.flObj in your test.

Timed nose tests not failing properly

I have the following test that does not fail when running an especially long fib assert.
Tests that don't fail properly
#!/usr/env/bin python2.7
import unittest
from fib import fib
from nose.tools import timed
def test_gen(expected, actual):
#timed(.001)
def test_method(self):
return self.assertEqual(expected, actual)
return test_method
if __name__ == '__main__':
all_cases = {
'user': ((fib(40), 102334155), (fib(2), 1), (fib(5), 5)),
}
fails = {}
for username, cases in all_cases.items():
class FibTests(unittest.TestCase):
pass
for index, case in enumerate(cases):
test_name = 'test_{0}_{1}'.format(username, index)
test = test_gen(case[1], case[0])
setattr(FibTests, test_name, test)
suite = unittest.TestLoader().loadTestsFromTestCase(FibTests)
result = unittest.TextTestRunner(verbosity=2).run(suite)
fails[username] = len(result.failures)
print fails
(Slow) Fib.py Implementation
def fib(x):
if x == 0:
return 0
elif x == 1:
return 1
return fib(x - 2) + fib(x - 1)
Tests that fail properly
import unittest
from fib import fib
from nose.tools import timed
def test_gen(expected, actual):
#timed(.001)
def test_method(self):
time.sleep(.2)
return self.assertEqual(expected, actual)
return test_method
You are timing the wrong thing, and never actually calling your test method. You are also going to an awful lot of effort to dynamically create and add your cases to your class that does nothing but act as a container for tests when nose supports generator test cases, which would be much easier to read and follow than what you have here. Also, is this a test file or a piece of product code? If it's a test file, then having all of that code in if __name__ == '__main__' is kind of odd, and if it is a product code file, then having the test_gen function and the unittest and nose import statements in the uncoditionally run part doesn't make much sense. I'd recommend doing it the following way, and not trying to make the test script self-runnable; just launch it with nose.
from fib import fib
from nose.tools import timed
fib = timed(.001)(fib)
def execute(username, fib_arg, expected_output):
result = fib(fib_arg)
assert result == expected_output, ('%s fib(%d) got %d, expected %d'
% (username, fib_arg, result, expected_output))
def test_fib():
for name, datasets in (('user', ((40, 102334155), (2, 1), (5, 5))),):
for arg, expected in datasets:
yield execute, name, arg, expected

Categories