I am trying to implement some unit-testing, and I have various different input files I want to call into my program to test. The function I want to test is located in my_file
, for a function called test_func
.
Here is my code:
import unittest
import make_html
class TestBuild(unittest.TestCase):
def test_test_func(self):
# Case 1: blank input file -> Return 'Blank'
expected_output = 'Blank'
self.assertEqual(make_html.test_func('blank'),expected_output)
# Case 2: Non-numeric -> Should raise ValueError
with self.assertRaises(ValueError):
make_html.test_func('non_numeric')
if __name__ == '__main__': # run the main file within unit test package
unittest.main()
When I try to run this unit test, it only ever runs 1 of the tests. If I run them separately, they return 'OK'. What am I doing wrong? How do I get it so both tests are run. Furthermore, I want to add even more cases. I am not really sure what it going in here, regarding the syntax surrounding class
, self
, __name__
and __main__
.
Update: I tried splitting it into two tests, but I am getting the same result:
import unittest
import make_html
class TestBuild(unittest.TestCase):
def test_test_func(self):
# Case 1: blank input file -> Return 'Blank'
expected_output = 'Blank'
self.assertEqual(make_html.test_func('blank'),expected_output)
def test_test_func(self):
# Case 2: Non-numeric -> Should raise ValueError
with self.assertRaises(ValueError):
make_html.test_func('non_numeric')
if __name__ == '__main__': # run the main file within unit test package
unittest.main()
My output is still just:
----------------------------------------------------------------------
Ran 1 test in 0.001s
You just need another test method:
class TestBuild(unittest.TestCase):
def test_blank(self):
# Case 1: blank input file -> Return 'Blank'
expected_output = 'Blank'
self.assertEqual(make_html.test_func('blank'),expected_output)
def test_ValueError(self):
# Case 2: Non-numeric -> Should raise ValueError
with self.assertRaises(ValueError):
make_html.test_func('non_numeric')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.