如何在编码完成后应用单元测试?

时间:2015-08-11 08:50:31

标签: python-2.7 unit-testing

我知道这是不好的做法(我想我发现了很难的方法),但我为公司制作了一个(相对)长的脚本来生成PDF文档,并将通过外部包生成的几个图像绘制到其上。所以这个脚本有很多依赖性,在某些部分有点复杂。

公司现在问我(他们在前面问过,但我以前从未测试过任何东西)来对我的代码进行单元测试,因此他们知道(至少是基础知识)已经过测试。我的问题:我以前从未做过任何测试,我在单元测试中查阅了一些阅读材料,我想我对它有了基本的了解。我现在面临的问题是所有示例都使用非常小的示例(例如添加数字并测试输出)。

现在我已经拥有一段复杂的代码,以后应用测试的最佳方法是什么?我已经创建了一个单独的测试脚本,如下所示:

import sys
sys.path.append('../')

from PTRGenerator import *
import unittest

class PTRTest(unittest.TestCase):
    """PTR Test case"""

    # preparing the tests
    def setUp(self):
        """ Setting up for the first test """
        print "PTRTest:setUp_:begin"
        # do something
        print "PTRTest:setUp_:end"

    # ending the test
    def tearDown(self):
        """Cleaning up after the test"""
        print "PTRTest:tearDown_:begin"
        # do something
        print "PTRTest:tearDown_:end"

    def testA(self):
        """Test routine A"""
        print "PTRTest:testA"

    def testB(self):
        """Test routine B"""
        print "PTRTest:testB"

if __name__ == "__main__":

    # creating a new testsuite
    newSuite = unittest.TestSuite()

    # adding a testcase
    newSuite.addTest(unittest.makeSuite(PTRTest))

一段“复杂”代码看起来像这样(我希望它根本不复杂):

# method to show footer.
def footer(canvas, doc):
    canvas.saveState()

    # footer consists of a table with 1 row, to make it look like the headers of the rest of the document
    footerTable = []
    footerTable.append(["PDF V%s" % jf['PDF_version'], "PAGE %i OF 2" %canvas.getPageNumber(), "%s" % time.strftime("%d-%b-%Y")])
    saveString = '%s.%s.%s.%i.pdf' %(jf['external_sample_id'], jf['study_name'], jf['our_sample_id'], args.id)
    footerTable.append(["%s" %saveString, "", "FOR RESEARCH USE ONLY"])

    rowHeight = 15

    ft = Table(footerTable, colWidths=(250, 50, 250), rowHeights=rowHeight)
    ft.setStyle(TableStyle([('FONT',    (0,0),  (-1,-1), HEADERS_FONT),
                ('FONTSIZE',    (0,0),  (-1, -1), HEADERS_FONT_SIZE),

                ## set boxes around appropriate cells
                # hotspot seq coverage box
                ('TEXTCOLOR',   (0, 0), (-1, -1), BOX_TEXT),
                ('BOX',     (0, 0), (-1, -1), 1, BOX_COLOR),
                ('BACKGROUND',  (0, 0), (-1, -1), BOX_COLOR),

                # Align the first line of the footer to the left, middle and right for elements 1 2 and 3
                ('ALIGN',   (0,0),  (0, 0), 'LEFT'),
                ('ALIGN',   (1,0),  (1, 0), 'CENTER'),
                ('ALIGN',   (2,0),  (2, 0), 'RIGHT'),

                # Align the second row
                #('SPAN',   (0,1),  (2, 1)),
                ('ALIGN',   (0,1),  (0, 1), 'LEFT'),
                ('ALIGN',   (2,1),  (2, 1), 'RIGHT'),
                ('VALIGN',  (0,0),  (-1, -1), 'MIDDLE')

                ]))

    ft.wrapOn(canvas, 100, 20)
    ft.drawOn(canvas, 22, 10)

    canvas.restoreState()

我如何知道检查复杂输出的值是什么?

如何检查图像的输出(图像是根据可变数据绘制的)?

如何检查依赖于其他库的元素类型(例如我有很多Reportlab对象)

1 个答案:

答案 0 :(得分:0)

打破你的复杂"编码成更简单的方法......单一的任务者。这些方法可以接受需要操作的args并返回处理结果。结合起来,这些单一的任务者将预先形成所有复杂的"工作

这些更简单的方法随后变得更容易测试。我是你的测试,你可以传入args并检查输出以确保正确完成处理。

您还可以使用示例文件进行测试。使用已知输入(图像等)生成已知输出(PDF)。现在您已经拥有了正常运行的代码,您可以使用测试输入来生成确认PDF。然后使用测试输入运行测试,并根据已知的有效输出验证输出(如使用MD5或SHA1校验和)。