【问题标题】:Change Environment Variables During Perl Unit Testing在 Perl 单元测试期间更改环境变量
【发布时间】:2014-02-18 15:25:14
【问题描述】:

我正在尝试为这个 perl 模块函数编写一些单元测试,但是在环境变量方面遇到了一些问题。我会先列出文件,然后更详细地解释问题。

processBuildSubs.pm

package processBuildSubs;
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Status;

# Declare environment variables used in this package that are needed
use constant URI_BASE     => $ENV {"URI_BASE"};
use constant URI_RESOURCE => $ENV {"URI_RESOURCE"};

# Shell Environment Related Constants visible.
# Make visible.

our $URI_BASE = URI_BASE;
our $URI_RESOURCE = URI_RESOURCE;

sub populatePartitions
{
  # Define locals
  my $url;
  my $ua = new LWP::UserAgent;

  $url = "$URI_BASE"."$URI_RESOURCE"."/some/path";

  # Make a request to the $url
  $res = $ua->request (GET $url);

  if ($res->code() != HTTP::Status->RC_OK() )
  {
     # The request didn't return 200 OK so it's in here now.
  }
  else
  {
     # The request returned 200 OK, so now it's here.
  }
}

我希望能够对if pathelse path 进行单元测试,但是,如果我根本不需要更改processBuildSubs.pm 代码,那将是最好的选择。这是一个我目前无法控制的外部文件。我只是负责对它进行单元测试(尽管我知道如果我们也可以更改源代码,它可以更有效地进行测试)。

所以为了测试两条路径,我们需要相应地设置环境变量URI_BASEURI_RESOURCE,这样请求一次失败,一次成功。 (我有兴趣了解如何在将来取消此呼叫,但这是为另一个问题保留的。)

这是我的测试文件:

processBuildSubs.t

use strict;
use Test::More qw(no_plan);

BEGIN { use_ok('processBuildSubs') };

# Test 1 of populatePartitions() function
my $processBuildProdsCall = processBuildSubs::populatePartitions();
is( $populatePartitionsCall, 0, "populatePartitions() Test for 0 Val Passed" );

# Test 2 of populatePartitions() function
# I need to change some environment variables that processBuildSubs depends on here.
my $processBuildProdsCall = processBuildSubs::populatePartitions();
is( $populatePartitionsCall, 0, "populatePartitions() Test for 0 Val Passed" );

我们现在更改环境变量的最佳尝试是使用外部 shell 脚本(但最好在上面文件中的 my 调用之间更改它们):

run_tests.sh

#!/bin/bash

# Run the tests once
perl ./BuildProcess.pl
perl ./Build testcover  # Ultimately calls the processBuildSubs.t test file

# Now export some variables so the other test passes.
export URI_BASE="https://some-alias/"
export URI_RESOURCE="some-resource"

# Then run the test suite again with the env set so the else condition passes.
perl ./BuildProcess.pl
perl ./Build testcover

如您所见,这将是一种糟糕的处理方式,因为我们每次都在不同的环境中运行整个测试套件。理想情况下,如果可能,我们希望在测试之间在 processBuildSubs.t 文件中设置我们的环境。

如果我可以提供更多信息,请告诉我。

【问题讨论】:

  • 您可以将run_tests.sh 替换为用perl 编写的脚本,因此%ENV 的更改会影响子PID。
  • 你可以使用 Class::Unload 吗?

标签: perl unit-testing environment-variables


【解决方案1】:

您是否反对为单独的测试环境使用单独的脚本?

# processBuildSubs.t
BEGIN {
   @ENV{"URI_BASE","URI_RESOURCE"} = ("https://some-alias/","some-resource");
}
use Test::More;
... tests go here ...

# processBuildSubs-env2.t
BEGIN {
   @ENV{"URI_BASE","URI_RESOURCE"} = ("https://another-alias/","another-resource");
}
use Test::More;
... tests go here ...

通过在BEGIN 块中设置%ENV,在加载任何其他模块之前,您可以在编译时为其他模块提供不同的环境变量。

【讨论】:

  • 嗯,这不是一个坏主意,我想我只是担心我会得到一百万个额外的文件。但这是一个很好的计划。我会考虑一下的。
猜你喜欢
  • 2018-12-18
  • 2023-03-29
  • 2017-10-04
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多