Home Python 运行单元测试unit Test Testcase
Post
Cancel

Python 运行单元测试unit Test Testcase

https://docs.python.org/3/library/unittest.html

非常简单:

class:

class VisionTool:
    def __init__(self):
        pass
    def get_current_player_cards(self, source_image_name):
pass

对应的测试文件

import unittest
from vision_tool import VisionTool

class TestVisionTool(unittest.TestCase):
    def setUp(self):
        self.vision_tool = VisionTool()

    def test_get_current_player_cards(self):
        # cards 1
        current_player_cards = self.vision_tool.get_current_player_cards('test/test_current_player_cards_1.png')
        expected_cards = [
                'A_spade', 'A_heart', 'A_diamond', 'K_heart',
                'Q_heart', 'Q_club', 'Q_diamond', 'J_diamond',
                'T_spade', 'T_heart', 'T_diamond', '9_club',
                '8_spade', '7_spade', '7_heart', '7_club',
                '6_spade', '5_spade', '4_club', '3_spade'
                ]
        expected_cards.sort()

        self.assertEqual(expected_cards, current_player_cards)

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

只运行某个单元测试;

https://stackoverflow.com/questions/15971735/running-a-single-test-from-unittest-testcase-via-the-command-line

python TestMyCase.py TestMyCase.testcase1

This post is licensed under CC BY 4.0 by the author.