python test code

Test when writing a function or class to make sure the code works

The python module unittest provides code testing tools.

Unit tests are used to verify that an aspect of a function is OK;

A test case is a set of unit tests that together verify that a function behaves as desired in each case

# Author:song
import unittest

class TestString(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('Foo'.upper(),'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s= 'hello world'
        self.assertEqual(s.split(),['hello','world'])
        with self.assertRaises(TypeError):
            s.split(2)


if __name__=="__main__":
    unittest.main()

  unittest.main():使用它可以将一个单元测试模块变为可直接运行的测试脚本,main()方法使用TestLoader类来搜索所有包含在该模块中以“test”命名开头的测试方法,并自动执行

 

  Python在unittest.TestCase类中提供了很多断言方法

常见几个

 

  unittest.TestCase类包含方法setUp(),让我们只需创建这些对象一 次,并在每个测试方法中使用它们。

AnonymousSurvey.py

class AnonymousSurvey():#创建匿名调查的类
    def __init__(self,question):
        self.question = question
        self.responses = []

    def show_question(self):
        print(self.question)

    def store_response(self,new_response):
        self.responses.append(new_response)

    def show_results(self):
        print('Survey result:')
        for response in self.responses:
            print('-'+response)

test code,

import unittest
class TestAnonymousSurvey(unittest.TestCase):
    def setUp(self):
        question = "What language did you first learn to speak?"
        self.my_survey = AnonymousSurvey(question)
        self.responses = ['English', 'Spanish', 'Mandarin']

    def test_store_single_response(self): #Test a single answer will be stored properly
        self.my_survey.store_response(self.responses[0])
        self.assertIn(self.responses[0], self.my_survey.responses)

    def test_store_three_responses(self): #Test that multiple answers are properly stored
         for response in self.responses:
            self.my_survey.store_response(response)
        for response in self.responses:
            self.assertIn(response, self.my_survey.responses)

unittest.main()

  The method setUp() does two things: it creates a survey object; it creates a list of answers. The variable names that store these two things include the prefix self (that is, stored in the property), and thus can be used anywhere in the class. This makes both test methods simpler because neither creates a survey object nor an answer. The method setUp() makes it easier to write test methods, which is much easier than creating an instance and setting its properties in each test method.

operation result

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

  When running a test case, Python prints a character each time a unit test completes: a period when the test passes; an E when the test raises an error; and an F when the test fails an assertion. That's why when you run the test case, you see a different number of periods and characters in the first line of the output. If your test case contains many unit tests that take a long time to run, you can observe these results to see how many tests passed

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324681127&siteId=291194637