0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】unittestサンプル

Last updated at Posted at 2021-09-17

次の sphinx_kana_text.py で定義されている KanaText クラスのユニットテスト。サンプルとして使えます。

#unittest_sphinx_kana_text.py

#!/usr/bin/python
import unittest
from sphinx_kana_text import KanaText

#正規表現による字句解析
testcase1 = [
[   #テストパターン
    "よみ1|用語1",
    "よみ2|用語2^120a3",
    "よみ3|用語3^",
    "よみ4|用語4^10a3あいうえお",
    "よみ5|用語5^あいうえお",
    "用語6",
    "用語7^120a3",
    "用語8^",
    "用語9^120a3あいうえお",
    "用語A^あいうえお",
    "  よみ1|用語1",
    "  よみ2|用語2^120a3",
    "  よみ3|用語3^",
    "  よみ4|用語4^10a3あいうえお",
    "  よみ5|用語5^あいうえお",
    "  用語6",
    "  用語7^120a3",
    "  用語8^",
    "  用語9^10a3あいうえお",
    "  用語A^あいうえお",
    "  よみ1|用語1",
    "  よみ2|用語2^120a3",
    "  よみ3|用語3^",
    "  よみ4|用語4^10a3あいうえお",
    "  よみ5|用語5^あいうえお",
    "  用語6",
    "  用語7^120a3",
    "  用語8^",
    "  用語9^10a3あいうえお",
    "  用語A^あいうえお", ],
[   #想定する結果
    "よみ1|用語1",
    "よみ2|用語2",
    "よみ3|用語3",
    "よみ4|用語4",
    "よみ5|用語5",
    "用語6",
    "用語7",
    "用語8",
    "用語9",
    "用語A",
    "よみ1|用語1",
    "よみ2|用語2",
    "よみ3|用語3",
    "よみ4|用語4",
    "よみ5|用語5",
    "用語6",
    "用語7",
    "用語8",
    "用語9",
    "用語A",
    "よみ1|用語1",
    "よみ2|用語2",
    "よみ3|用語3",
    "よみ4|用語4",
    "よみ5|用語5",
    "用語6",
    "用語7",
    "用語8",
    "用語9",
    "用語A", ]]

#オプションの処理
testcase2 = [
[   #テストパターン
    "よみ1|用語1",
    "よみ2|用語2^120a3",
    "よみ3|用語3^",
    "よみ4|用語4^10a3あいうえお",
    "よみ5|用語5^あいうえお",
    "用語6",
    "用語7^120a3",
    "用語8^",
    "用語9^120a3あいうえお",
    "用語A^あいうえお", ],
[   #想定する結果
    [[(False, '用語1')]],
    [[(True, ('', '')), (True, ('', 'み2')), (False, '')]],
    [[(True, ('用語3', 'よみ3'))]],
    [[(True, ('', '')), (False, ''), (False, '')]],
    [[(True, ('用語5', 'よみ5'))]],
    [[(False, '用語6')]],
    [[(False, '用語7')]],
    [[(False, '用語8')]],
    [[(False, '用語9')]],
    [[(False, '用語A')]], ]]

#オプションと文字データの文字数の多少
testcase3 = [
[   #テストケース
    'いろはにほへと|壱弐参四五六七八九',
    'いろはにほへと|壱弐参四五六七八九^',
    'いろはにほへと|壱弐参四五六七八九^111010111',
    'いろはにほへと|壱弐参四五六七八九^1201012',
    'いろはにほへと|壱弐参四五六七八九^33',
    'いろはにほへと|壱弐参四五六七八九^45',
    'いろはにほへと|壱弐参四五六七八九^55',
    'いろはにほへと|壱弐参四五六七八九^0000000000',
    'いろはにほへと|壱弐参四五六七八九^i9',
    'いろはにほへと|壱弐参四五六七八九^jkl7',
    'いろはにほへと|壱弐参四五六七八九^jkl9',
    'いろはにほへと|壱弐参四五六七八九^jkl55',
        ], 
[   #期待する結果
    [[(False, '壱弐参四五六七八九')]],
    [[(True, ('壱弐参四五六七八九', 'いろはにほへと'))]],
    [[  (True, ('', '')),
        (True, ('', '')),
        (True, ('', '')),
        (False, ''),
        (True, ('', '')),
        (False, ''),
        (True, ('', '')),
        (True, ('', '')),
        (True, ('', ''))]],
    [[  (True, ('', '')),
        (True, ('', 'ろは')),
        (False, ''),
        (True, ('', '')),
        (False, ''),
        (True, ('', '')),
        (True, ('', 'へと')),
        (False, '八九')]],
    [[(True, ('', 'いろは')), (True, ('', 'にほへ')), (False, '参四五六七八九')]],
    [[(True, ('', 'いろはに')), (True, ('', 'ほへと')), (False, '参四五六七八九')]],
    [[(True, ('', 'いろはにほ')), (True, ('', 'へと')), (False, '参四五六七八九')]],
    [[  (False, ''), (False, ''), (False, ''), (False, ''), (False, ''),
        (False, ''), (False, ''), (False, ''), (False, '')]],
    [[(False, '壱弐参四五六七八九')]],
    [[(True, ('', 'いろはにほへと')), (False, '弐参四五六七八九')]],
    [[(True, ('', 'いろはにほへと')), (False, '弐参四五六七八九')]],
    [[(True, ('', 'いろはにほ')), (True, ('', 'へと')), (False, '参四五六七八九')]],
]]

#用語の数
testcase4 = [
[   #テストケース
    '',
    r'いろはにほへと|壱弐参四五六七八九^',
    r'いろはに|壱弐参四; ほへと|五六七八九^',
    r'いろは|壱弐参; にほへ|四五六^; とちり|七八九',
    r'いろ|壱弐; はに|参四^; ほへ|五六; とち|七八九^',
    r'いろ|壱弐; はに|参四^; ほへ|五六; とち|七八^; りぬ|九拾',
        ], 
[   #期待する結果
    [None],
    [[(True, ('壱弐参四五六七八九', 'いろはにほへと'))]],
    [[(False, '壱弐参四')], [(True, ('五六七八九', 'ほへと'))]],
    [[(False, '壱弐参')], [(True, ('四五六', 'にほへ'))], [(False, '七八九')]],
    [   [(False, '壱弐')], [(True, ('参四', 'はに'))],
        [(False, '五六')], [(True, ('七八九', 'とち'))]],
    [   [(False, '壱弐')], [(True, ('参四', 'はに'))], [(False, '五六')],
        [(True, ('七八', 'とち'))], [(False, '九拾')]],
]]

#正規表現による字句解析
class Test1KanaText(unittest.TestCase):
    def test_HTMLTextNode(self):
        for t, e in zip(testcase1[0], testcase1[1]):
            node = KanaText(t)
            rslt = node.astext()
            self.assertEqual(e, rslt)

#オプションの処理
class Test2KanaText(unittest.TestCase):
    def test_KanaText(self):
        for t, e in zip(testcase2[0], testcase2[1]):
            node = KanaText(t)
            rslt = node.aslist()
            self.assertEqual(e, rslt)

#オプションと文字データの文字数の多少
class Test3KanaText(unittest.TestCase):
    def test_KanaText(self):
        for t, e in zip(testcase3[0], testcase3[1]):
            node = KanaText(t)
            rslt = node.aslist()
            self.assertEqual(e, rslt)

#用語の数
class Test4KanaText(unittest.TestCase):
    def test_KanaText(self):
        for t, e in zip(testcase4[0], testcase4[1]):
            node = KanaText(t)
            rslt = node.aslist()
            self.assertEqual(e, rslt)

#KanaText.force_to_parse

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

#以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?