Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the use of Python unit testing

Shulou Source: shulou.com Published: 2022-05-31 10:44:23 10月01日 Update

This article mainly introduces the relevant knowledge of the use of Python unit testing, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Python unit test article. Let's take a look at it.

Description

1. Unit testing ensures that the behavior of the program module conforms to the test cases we designed. When you modify it in the future, you can greatly guarantee that the behavior of the module is correct.

2. Unit testing can effectively test the behavior of a program module, which is the confidence guarantee of refactoring code in the future.

Example

Class TestStudent (unittest.TestCase): def test_80_to_100 (self): S1 = Student ('Bart', 80) S2 = Student (' Lisa', 100) self.assertEqual (s1.get_grade (),'A') self.assertEqual (s2.get_grade (),'A') def test_60_to_80 (self): S1 = Student ('Bart', 60) S2 = Student (' Lisa') 79) self.assertEqual (s1.get_grade (),'B') self.assertEqual (s2.get_grade (),'B') def test_0_to_60 (self): S1 = Student ('Bart', 0) S2 = Student (' Lisa', 59) self.assertEqual (s1.get_grade (),'C') self.assertEqual (s2.get_grade () 'C') def test_invalid (self): S1 = Student ('Bart',-1) S2 = Student (' Lisa' With self.assertRaises (ValueError): s1.get_grade () with self.assertRaises (ValueError): s2.get_grade () if _ _ name__ = ='_ _ main__': unittest.main () this is the end of the article on "what's the use of Python unit testing"? Thank you for reading! I believe you all have a certain understanding of the knowledge of "what is the use of Python unit testing". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Tests units modules knowledge behavior content programs program modules articles guarantees effectiveness code value confidence examples easy to operate articles easy to understand more read Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Microsoft MySQL Xiaomi