【问题标题】:How can I write a unittest for this class?我怎样才能为这个类写一个单元测试?
【发布时间】:2018-08-15 19:25:15
【问题描述】:

所以我对编写单元测试非常陌生,我需要一些关于如何为我编写的课程编写单元测试的指导。 任何帮助将不胜感激 提前致谢!另外,我想在 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)

【问题讨论】:

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


    【解决方案1】:

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

    这里不需要一个类,只需要一个字典和一些函数。

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

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 2023-02-11
      • 1970-01-01
      • 2021-10-06
      • 2020-10-10
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      相关资源
      最近更新 更多