如何在Perl中创建单元测试?

时间:2016-03-14 18:57:02

标签: perl unit-testing subroutine

我正在尝试为下面显示的此错误后子程序创建单元测试。此方法接收错误名称并打印出消息,该消息是位于每个错误中的值。这是我的代码:

    use constant {
        # link included as a variable in this example
        ERROR_AED => {
        errorCode => 561,
        message => {"this is an error. "},
        tt => { template => 'disabled'},
        link => 'www.error-fix.com',
        },
};


sub error_post {

    my($error) = @_;
    printf ($error->{ message });    
}

error_post(ERROR_AED);

这是我的方法我非常确定我试图验证输入值是错误的,或者更一般地确认它是传递给error_post方法的错误。

#verifying input values 

sub test_error_post {
    ok( defined $error, 'Should have an input value' );  # check that it's a constant
    ok($error->isa(constant) , 'Error should be of type constant');
    ok($error->errorCode), 'Should contain key errorCode');
    ok($error->message), 'Should contain key message'); 
    ok($error->tt), 'Should contain key tt');
    ok($error->wiki_page) 'Should contain key wiki page');      
}

我知道这可能还有很长的路要走。

1 个答案:

答案 0 :(得分:1)

我没有太多时间,但这里有一些关于我如何写它们的测试。但是,测试应该存在于自己的文件中,具有display:flex;扩展名,并且位于自己的目录中。它们不应该与您正在编写的代码一致。

*.t