我该如何为此类编写单元测试?

时间:2018-08-15 19:25:15

标签: python-3.x pycharm python-unittest

因此,我对编写单元测试非常陌生,我需要一些有关如何为已编写的类编写单元测试的指南。 任何帮助将不胜感激 提前致谢!另外,我想用pycharm编写单元测试。

class StringProcessor:
    def __init__(self, yo="", a="", b="", f=""):
        self.yo = yo
        self.a = a
        self.b = b
        self.f = f

    def my_first_test(self):
        yo = input("Enter your string here")
        ya = yo.split()
        even = 0
        odd = 0
        for i in ya:
            if len(i) % 2 == 0:
                even = even + 1
            else:
                odd = odd + 1
        print("The number of odd words are ", odd)
        print("The number of even words are", even)

    def my_second_test(self, a, b):
            d = a.split()
            print("The word", b, "is repeated: ", d.count(b), "times")

    def my_third_test(self, f):
        d = {}
        lst = f.split()
        for c in lst:
            d[c] = lst.count(c)
        for key,value in d.items():
            print(key, ":", value)

1 个答案:

答案 0 :(得分:0)

您必须将用户输入与类分开,以使其可测试。

您在这里不需要上课,只需一则字典和一些功能。

此外,恕我直言,用户输入是邪恶的,请改用sys.argv