我如何做Perl机器或平台相关的TDD?

时间:2022-07-12 12:12:17

How do I go about testing a function or module that is machine or platform dependent? For example, something that looks at/depends on $^O or a module like Net::Ifconfig::Wrapper? I don't need to test that Net::Ifconfig::Wrapper is returning the correct values, but I do need to test whether or not I'm doing the right thing with those values.

如何测试依赖于机器或平台的功能或模块?例如,查看/依赖于$ ^ O或Net :: Ifconfig :: Wrapper等模块的东西?我不需要测试Net :: Ifconfig :: Wrapper返回正确的值,但我确实需要测试我是否正在使用这些值做正确的事情。

Thanks!

EDIT: Testing $^O turned out to be easier than I thought:

编辑:测试$ ^ O结果比我想象的更容易:

{
    # <~> $ perl -e 'print $^O'
    # linux

    local $^O = 'linux';
    $rc = GetOSType();
    is($rc, $OS_LINUX, 'OS Check - linux');
}

For some reason I thought it was a read-only variable.

出于某种原因,我认为这是一个只读变量。

2 个解决方案

#1


To follow up on Jason's mock objects suggestion, you should take a look at the article "Interfacing with hard-to-test third party controls" by Misko Hevery. It directly addresses testing third party components.

为了跟进Jason的模拟对象建议,你应该看看Misko Hevery的文章“与难以测试的第三方控件接口”。它直接解决测试第三方组件的问题。

#2


Generally you use mock objects to "fake up" the results of those system calls.

通常,您使用模拟对象来“伪造”这些系统调用的结果。

During testing, use mock objects and have them return the various results you'd expect on different platforms, testing that your code reacts properly in those cases.

在测试期间,使用模拟对象并让它们返回您在不同平台上期望的各种结果,测试您的代码在这些情况下是否正确反应。

#1


To follow up on Jason's mock objects suggestion, you should take a look at the article "Interfacing with hard-to-test third party controls" by Misko Hevery. It directly addresses testing third party components.

为了跟进Jason的模拟对象建议,你应该看看Misko Hevery的文章“与难以测试的第三方控件接口”。它直接解决测试第三方组件的问题。

#2


Generally you use mock objects to "fake up" the results of those system calls.

通常,您使用模拟对象来“伪造”这些系统调用的结果。

During testing, use mock objects and have them return the various results you'd expect on different platforms, testing that your code reacts properly in those cases.

在测试期间,使用模拟对象并让它们返回您在不同平台上期望的各种结果,测试您的代码在这些情况下是否正确反应。