TestDrivedDevelopment is a test-driven development method. this development mode is not just a test, but a means to break down requirements and ensure code quality. A unit test is a test of a module, a function, or a class for correctness detection.
Test Drived Development is a Test-driven Development method. this Development mode is not just a Test, but a means to break down requirements and ensure code quality.
Unit testing not only ensures that functions, modules, or classes meet our needs during testing. At the same time, it is also helpful for us to modify classes or functions in the future. we only need to run the unit test again to ensure that our modifications will not affect the function.
Of course, unit testing also greatly increases the amount of code.
To compile unit test code, you must introduce the Python unittest package. We can create a unit test class, but this class must inherit the unitest. TestCase class. The test method in the test class must start with test, otherwise it will not be executed. Then we can use the methods in unitest. TestCase. Common methods include:
AssertEqual (self, first, second) # determine whether it is an expected value
AssertTrue (bool) # determine whether it is true
AssertRaises (exception) # determines whether a non-throw exception is thrown.
Let's take an example:
We first define a class custom class and then perform unit tests on it.
Dict. py
class Dict(dict):def __init__(self, **kwargs):super(Dict, self).__init__(**kwargs)def __getattr__(self, item):try:return self[item]except KeyError:raise AttributeErrordef __setattr__(self, key, value):self[key] = valueimport unittestfrom Dict import Dictclass DictTest(unittest.TestCase):def test_init(self):d = Dict(a=100, b=200)self.assertEqual(d.a, 100)self.assertEqual(d.b, 200)self.assertTrue(isinstance(d, dict))def test_key(self):d = Dict()d['key'] = "300"self.assertEqual(d["key"], "300")def test_attar(self):d = Dict()d.attar = "attar"self.assertTrue("attar" in d)self.assertEqual(d.attar, "attar")def test_keyError(self):d = Dict()with self.assertRaises(KeyError):value = d["NotExisted"]def test_attarError(self):d = Dict()with self.assertRaises(AttributeError):value = d.not_existedif __name__ == '__main__':unittest.main()
Because we define a subclass of dict, we need to test its key and value. at the same time, it is a class, so we also need to test its attribute.
We define the unittest. TestCase Subclass. we need to write two sentences:
if __name__ == '__main__':unittest.main()
We can execute it just like a normal python file.
In addition, we can also execute multiple test
Python-m unittest DictTest
SetUp and tearDown functions.
The setUp and tearDown functions are two special functions. SetUp is run before all unit tests start; tearDown function is run after all unit tests start. The application scenarios of these two functions are as follows: for example, you can start the connection in the setUp function and close the connection in the tearDown function when you need to connect to the database.
The above is the unit test and UnitTest content for the python study notes. For more information, see The PHP Chinese website (www.php1.cn )!