Python类中的测试用例

时间:2016-09-21 03:02:59

标签: python-2.7

我有一个课程作业,我试图理解周围的框架。

我创建了一个StringCalculator类模块,然后创建了一个测试文件。

在为我的类模块运行而创建的测试文件中,我有许多“方法”。一般来说,这些“方法”会被视为一个测试案例,还是应该像doctest一样?

请原谅我,因为我是编程新手,我很难搞清楚周围的框架和测试用例。所以请不要搁置这个帖子或任何事情。我愿意回答你提出的任何问题。再次,我是一个新手。我只需要知道我在代码中概述的内容是否被视为测试用例。

下面的代码就是我的测试文件:

import unittest
from StringCalculatorClass2 import StringCalculator

class TestStringCalculatorClass(unittest.TestCase):

    def test_add_empty_string(self):
        string_calculator = StringCalculator()
        result = string_calculator.add("")
        assert 0 == result

    def test_add_one(self):
        string_calculator = StringCalculator()
        result = string_calculator.add("1")
        assert 1 == result

    def test_add_two(self):
        string_calculator = StringCalculator()
        result = string_calculator.add("2")
        assert 2 == result

    def test_add_one_and_two(self):
        string_calculator = StringCalculator()
        result = string_calculator.add("1,2")
        assert 3 == result

    def test_add_five_numbers(self):
        string_calculator = StringCalculator()
        result = string_calculator.add("1,2,17,4,1,1")
        assert 26 == result

    def test_add_with_new_line(self):
        string_calculator = StringCalculator()
        result = string_calculator.add("1,2\n17")
        assert 20 == result

    def test_is_new_delimiter_set(self):
        string_calculator = StringCalculator()
        result1 = string_calculator._is_new_delimiter_set("//;\n1;2;17\n5")
        result2 = string_calculator._is_new_delimiter_set("1;2;17\n5")
        assert True == result1
        assert False == result2

    def test_get_new_delimiter(self):
        string_calculator = StringCalculator()
        result = string_calculator._get_new_delimiter("//;\n1;2;17\n5")
        assert ';' == result

    def test_get_string_without_delimiter(self):
        string_calculator = StringCalculator()
        result = string_calculator._get_string_without_delimiter("//;\n1,2")
        assert "1,2" == result

    def test_call_add_with_new_delimiter(self):
        string_calculator = StringCalculator()
        result = string_calculator.add("//;\n1;2;17\n5")
        assert 25 == result

    def test_create_error_message_for_negative_numbers(self):
        string_calculator = StringCalculator()
        negative_numbers = [-1, -2, -3]
        result = string_calculator._create_error_message(negative_numbers)
        assert 'negatives not allowed: -1, -2, -3' == result

    def test_negative_numbers_raises_exception(self):
        string_calculator = StringCalculator()
        exception_raised = False
        try:
            result = string_calculator.add("-1")
        except Exception:
            exception_raised = True
        assert True == exception_raised

    def test_negative_number_raises_exception_with_message(self):
        string_calculator = StringCalculator()
        exception_raised = False
        exception_message = ""
        try:
            result = string_calculator.add("-1")
        except Exception as err:
            exception_message = err
            exception_raised = True
        assert True == exception_raised
        assert 'negatives not allowed: -1' == exception_message.args[0]

    def test_multiple_negative_numbers(self):
        string_calculator = StringCalculator()
        exception_raised = False
        exception_message = ""
        try:
            result = string_calculator.add("2,-1,4,-2")
        except Exception as err:
            exception_message = err
            exception_raised = True
        assert True == exception_raised
        assert 'negatives not allowed: -1, -2' == exception_message.args[0]

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

1 个答案:

答案 0 :(得分:0)

是的,这些方法算作测试用例。正确的方法是使用unittest.TestCase类的断言函数,而不是使用默认的python assert。因此,例如,对于第一个测试,您应该拥有以下代码:

def test_add_empty_string(self):
    string_calculator = StringCalculator()
    result = string_calculator.add("")
    self.assertEqual(result, 0)

unittest.TestCase类中存在各种断言,例如assertTrueassertFalse,...

您可以在此处详细了解单元测试:https://docs.python.org/2/library/unittest.html