Change Environment Variables During Perl Unit Testing -


I am trying to write some unit tests for this perl module function, but I have some problems with the environmental variable I will list the files first, then explain this issue in more detail.

processBuildSubs.pm

  Process of packageBilidsSub; Strict use; Use warnings; Use LWP :: UserAgent; HTTP :: Request :: Use common; Use HTTP :: status; # Announce the environment variables to be used in this package that are continuously URI_BASE = & gt; $ ENV {"URI_BASE"}; Continuous URI_RESOURCE = & gt; $ ENV {"URI_RESOURCE"}; # Shell Environmental Related Stable Visual # Make our $ URI_BASE = URI_BASE; Our $ URI_RESOURCE = URI_RESOURCE; Sub populatePartitions {# local defined my $ url; My $ UA = new LWP :: user agent; $ Url = "$ URI_BASE" "$ URI_RESOURCE". "/ Some / path"; # $ Url $ res = $ ua- & gt; Request to request (get $ url); If ($ res-> code ()! = HTTP :: status-> RC_OK ()) {# request was not returned 200 OK, so it's here right now} and {# request returned back to 200 OK , So now it is here}}  

I want to be able to test the unit if path and another path , though , It would be best for me if I do not need to change the processBuildSubs.pm code at all. This is an external file that I currently do not have control. I have been entrusted with just a unit test (although I think it can be tested more efficiently, if we can also change the source code).

Therefore, to test both methods, we need environment variables URI_BASE and URI_RESOURCE are set accordingly, so that the request is made once Fails, and becomes successful for any other time. (I am interested in learning how to tap this call in the future, but it is reserved for another question.)

Here is my test file:

processBuildSubs .t

  Strictly use; Test :: Use more qw (no_plan); BEGIN {use_ok ('processbuildSubs''}}; 1 Test of #Putual Strips () Build My $ ProcessBillProdscall = ProcessedBlue :: PopulatePrint (); is ($ Populated Parts Call, 0, "PopularPrint () 0 Val Pass Test "); #Test2 () Function # of populate bars # I need to change some environmental variables that builds trusts here. My $ ProcessBoldProodesCalls = ProcessBuild Abou :: Populate Reputation (); ($ Populated Parts Call, 0, "Populate Repetition (0) Test for Val Pass");  

We still do the best to change the environmental variable Efforts are using an external shell script such as (but call it in the my file above):

run_tests.sh

  #! / Bin / bash # run perl / Bildprocess test once. Pl perl ./Build testcover # Finally calls process BiwildSubs.t test file # Now export some variables so that NY passed the test. Export URI_BASE = "https: // play with a nickname /" Export URI_RESOURCE = "some resources" # re-examination room again Anwa set to have the second position. Perl ./BuildProcess.pl perl ./Build testcover  

As you can see, this would be a bad way of working, because we run the entire trial suite with different environments Are there. Ideally we want to setup our environment between tests possibly in the processBuildSubs.t file.

Please tell me if I can provide more information.

Are you protesting against different scripts for different test environments?

  # processingBildSubs .t BEGIN {@ENV {"URI_BASE", "URI_RESOURCE"} = ("https: // some alias /", "some resources"); } Test :: Use more; ... exams go here ...  

  # processed subs-env2.t BEGIN {@ENV {"URI_BASE", "URI_RESOURCE"} = ("https : // one-aka / "," another resource "); } Test :: Use more; ... Examinations go here ...   

% ENV before loading any other module, in a BEGIN block By setting up, you make your other modules various environment variables available at compile time.


Comments