SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
Testing Scripts
                      Randal L. Schwartz, merlyn@stonehenge.com
                             Version LT-1.05 on 13 Jun 2012

                         This document is copyright 2012 by Randal L. Schwartz, Stonehenge Consulting Services, Inc.
                 This work is licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
                                               http://creativecommons.org/licenses/by-nc-sa/3.0/




Monday, June 25, 12                                                                                                        1
• Problem:
                       • Ya gotta test!
                      • Solution:
                       • use Test::More and friends
                      • But:
                       • What about scripts!


Monday, June 25, 12                                   2
• Problem:
                       • scripts are separate process
                       • hard to mock things
                      • Solution:
                       • Don’t use a separate process
                       • Require your script in your .t
                      • But:
                       • How will I invoke it then?

Monday, June 25, 12                                       3
• Problem:
                       • Loose code is effectively “main”
                      • Solution:
                       • Bundle loose code into a run subroutine:
                          sub run { ... }
                       • Also ensure true value at end of script
                      • But:
                       • What will invoke “run” then?

Monday, June 25, 12                                                 4
• Problem:
                       • Invoke “run” when run normally
                       • Don’t invoke “run” via require
                      • Solution:
                       • Use “caller”:
                          run(@ARGV) unless caller;
                      • But:
                       • What about namespace of .t file

Monday, June 25, 12                                       5
• Problem:
                       • Collision between script and .t names
                      • Solution:
                       • Bring it into its own package:
                            BEGIN {
                              package Program;
                              require "your-script";
                              die $@ if $@;
                            }
                      •   But:
                          • How to “invoke the program” from tests?


Monday, June 25, 12                                                   6
• Problem:
                       • Simulate execution
                      • Solution:
                       • Invoke run() with desired @ARGV:
                            subtest try_it => sub {
                              Program::run(qw(--foo --bar abc));
                            };
                      •   But:
                          • What about exceptions, exit, stdout?



Monday, June 25, 12                                                7
• Problem:
                       • Trapping everything (not just die)
                       • eval doesn’t cut it!
                      • Solution:
                       • Test::Trap!
                            use Test::Trap;
                            trap {
                              Program::run(qw(--foo --bar abc));
                            };
                      •   But:
                          • How will I know how the code finished?


Monday, June 25, 12                                                 8
• Problem:
                       • Was it normal exit, “exit”, or die?
                      • Solution:
                       • examine $trap object after trap { .. }
                            ok $trap->exit, 0, "exited 0";
                            like $trap->die, qr{missing args};
                      •   But:
                          • What about stdout, stderr, warnings?




Monday, June 25, 12                                                9
• Problem:
                       • What about those outputs?
                      • Solution:
                       • Test::Trap captures those too!
                            like $trap->stdout, qr{usage};
                            is $trap->stderr, q{}, "quiet errors";
                            is @{$trap->warn}, 1, "exactly 1 warn";
                      •   But:
                          • What about stubbing or mocking?



Monday, June 25, 12                                                   10
• Problem:
                       • Want to override some behavior
                      • Solution:
                       • Monkey patching!
                            subtest stub_it => sub {
                              local *Program::some_sub = sub { ... };
                              trap { Program::run() };
                            };
                      •   But:
                          • What about stdin?


Monday, June 25, 12                                                     11
• Problem:
                       • Provide stdin for script
                      • Solution:
                       • Small matter of programming:
                            local *STDIN;
                            open STDIN, "<", (my $S = join(q{}));
                            $$S .= "onentwonthreen";
                            trap { ... };
                            $$S .= "fournfiven"; trap { ... };
                      •   But:
                          • What about chdir?


Monday, June 25, 12                                                  12
• Problem:
                       • chdir has global effect
                      • Solution:
                       • Test::Trap is pluggable!
                          use Test::Trap::mine qw(:cwd);
                          trap { chdir "/tmp"; Program::run() };
                        • See my blog, or might be core now
                      • But:
                        • Does this really work for all scripts



Monday, June 25, 12                                                13
• Problem:
                       • Script might need complex interaction
                       • Maybe can’t edit code into run()
                       • Code might fork
                      • Solution:
                       • Yeah, traditional subprocesses
                       • Perhaps combined with Expect
                      • But:
                       • Test::Trap is amazingly useful!

Monday, June 25, 12                                              14
Follow me

                      •   Twitter: @merlyn
                      •   G+: Randal L. Schwartz
                      •   Personal blog: merlyn.posterous.com
                      •   http://blogs.perl.org/users/randal_l_schwartz/
                      •   merlyn, realmerlyn, or RandalSchwartz




Monday, June 25, 12                                                        15

Más contenido relacionado

Similar a Testing scripts

OptView2 - C++ on Sea 2022
OptView2 - C++ on Sea 2022OptView2 - C++ on Sea 2022
OptView2 - C++ on Sea 2022Ofek Shilon
 
Rubinius - A Tool of the Future
Rubinius - A Tool of the FutureRubinius - A Tool of the Future
Rubinius - A Tool of the Futureevanphx
 
How to really obfuscate your pdf malware
How to really obfuscate   your pdf malwareHow to really obfuscate   your pdf malware
How to really obfuscate your pdf malwarezynamics GmbH
 
How to really obfuscate your pdf malware
How to really obfuscate your pdf malwareHow to really obfuscate your pdf malware
How to really obfuscate your pdf malwarezynamics GmbH
 
Down the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM WonderlandDown the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM WonderlandCharles Nutter
 
Test First Teaching
Test First TeachingTest First Teaching
Test First TeachingSarah Allen
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplainedshannomc
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpMichael Girouard
 
Rihards Olups - Zabbix at Nokia - Case Study
Rihards Olups - Zabbix at Nokia - Case StudyRihards Olups - Zabbix at Nokia - Case Study
Rihards Olups - Zabbix at Nokia - Case StudyZabbix
 
Code Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured ExceptionsCode Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured ExceptionsJohn Anderson
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScriptSimon Willison
 
Adventures in Femtoland: 350 Yuan for Invaluable Fun
Adventures in Femtoland: 350 Yuan for Invaluable FunAdventures in Femtoland: 350 Yuan for Invaluable Fun
Adventures in Femtoland: 350 Yuan for Invaluable Funarbitrarycode
 
owasp lithuania chapter - exploit vs anti-exploit
owasp lithuania chapter - exploit vs anti-exploitowasp lithuania chapter - exploit vs anti-exploit
owasp lithuania chapter - exploit vs anti-exploitKęstutis Meškonis
 

Similar a Testing scripts (20)

OptView2 - C++ on Sea 2022
OptView2 - C++ on Sea 2022OptView2 - C++ on Sea 2022
OptView2 - C++ on Sea 2022
 
Rubinius - A Tool of the Future
Rubinius - A Tool of the FutureRubinius - A Tool of the Future
Rubinius - A Tool of the Future
 
Getting testy with Perl
Getting testy with PerlGetting testy with Perl
Getting testy with Perl
 
How to really obfuscate your pdf malware
How to really obfuscate   your pdf malwareHow to really obfuscate   your pdf malware
How to really obfuscate your pdf malware
 
How to really obfuscate your pdf malware
How to really obfuscate your pdf malwareHow to really obfuscate your pdf malware
How to really obfuscate your pdf malware
 
Down the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM WonderlandDown the Rabbit Hole: An Adventure in JVM Wonderland
Down the Rabbit Hole: An Adventure in JVM Wonderland
 
Test-Tutorial
Test-TutorialTest-Tutorial
Test-Tutorial
 
Test-Tutorial
Test-TutorialTest-Tutorial
Test-Tutorial
 
Leiningen
LeiningenLeiningen
Leiningen
 
Anti-patterns
Anti-patternsAnti-patterns
Anti-patterns
 
How to-node-core
How to-node-coreHow to-node-core
How to-node-core
 
Test First Teaching
Test First TeachingTest First Teaching
Test First Teaching
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplained
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Rihards Olups - Zabbix at Nokia - Case Study
Rihards Olups - Zabbix at Nokia - Case StudyRihards Olups - Zabbix at Nokia - Case Study
Rihards Olups - Zabbix at Nokia - Case Study
 
Code Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured ExceptionsCode Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured Exceptions
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScript
 
Intro to io
Intro to ioIntro to io
Intro to io
 
Adventures in Femtoland: 350 Yuan for Invaluable Fun
Adventures in Femtoland: 350 Yuan for Invaluable FunAdventures in Femtoland: 350 Yuan for Invaluable Fun
Adventures in Femtoland: 350 Yuan for Invaluable Fun
 
owasp lithuania chapter - exploit vs anti-exploit
owasp lithuania chapter - exploit vs anti-exploitowasp lithuania chapter - exploit vs anti-exploit
owasp lithuania chapter - exploit vs anti-exploit
 

Más de Randal Schwartz

Native mobile application development with Flutter (Dart)
Native mobile application development with Flutter (Dart)Native mobile application development with Flutter (Dart)
Native mobile application development with Flutter (Dart)Randal Schwartz
 
Git: a brief introduction
Git: a brief introductionGit: a brief introduction
Git: a brief introductionRandal Schwartz
 
A brief introduction to dart
A brief introduction to dartA brief introduction to dart
A brief introduction to dartRandal Schwartz
 
Intro to git (one hour version)
Intro to git (one hour version)Intro to git (one hour version)
Intro to git (one hour version)Randal Schwartz
 

Más de Randal Schwartz (10)

Why Flutter.pdf
Why Flutter.pdfWhy Flutter.pdf
Why Flutter.pdf
 
Native mobile application development with Flutter (Dart)
Native mobile application development with Flutter (Dart)Native mobile application development with Flutter (Dart)
Native mobile application development with Flutter (Dart)
 
Git: a brief introduction
Git: a brief introductionGit: a brief introduction
Git: a brief introduction
 
Perl best practices v4
Perl best practices v4Perl best practices v4
Perl best practices v4
 
A brief introduction to dart
A brief introduction to dartA brief introduction to dart
A brief introduction to dart
 
My half life with perl
My half life with perlMy half life with perl
My half life with perl
 
Intro to git (one hour version)
Intro to git (one hour version)Intro to git (one hour version)
Intro to git (one hour version)
 
Introduction to git
Introduction to gitIntroduction to git
Introduction to git
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
Forget The ORM!
Forget The ORM!Forget The ORM!
Forget The ORM!
 

Último

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Testing scripts

  • 1. Testing Scripts Randal L. Schwartz, merlyn@stonehenge.com Version LT-1.05 on 13 Jun 2012 This document is copyright 2012 by Randal L. Schwartz, Stonehenge Consulting Services, Inc. This work is licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License http://creativecommons.org/licenses/by-nc-sa/3.0/ Monday, June 25, 12 1
  • 2. • Problem: • Ya gotta test! • Solution: • use Test::More and friends • But: • What about scripts! Monday, June 25, 12 2
  • 3. • Problem: • scripts are separate process • hard to mock things • Solution: • Don’t use a separate process • Require your script in your .t • But: • How will I invoke it then? Monday, June 25, 12 3
  • 4. • Problem: • Loose code is effectively “main” • Solution: • Bundle loose code into a run subroutine: sub run { ... } • Also ensure true value at end of script • But: • What will invoke “run” then? Monday, June 25, 12 4
  • 5. • Problem: • Invoke “run” when run normally • Don’t invoke “run” via require • Solution: • Use “caller”: run(@ARGV) unless caller; • But: • What about namespace of .t file Monday, June 25, 12 5
  • 6. • Problem: • Collision between script and .t names • Solution: • Bring it into its own package: BEGIN { package Program; require "your-script"; die $@ if $@; } • But: • How to “invoke the program” from tests? Monday, June 25, 12 6
  • 7. • Problem: • Simulate execution • Solution: • Invoke run() with desired @ARGV: subtest try_it => sub { Program::run(qw(--foo --bar abc)); }; • But: • What about exceptions, exit, stdout? Monday, June 25, 12 7
  • 8. • Problem: • Trapping everything (not just die) • eval doesn’t cut it! • Solution: • Test::Trap! use Test::Trap; trap { Program::run(qw(--foo --bar abc)); }; • But: • How will I know how the code finished? Monday, June 25, 12 8
  • 9. • Problem: • Was it normal exit, “exit”, or die? • Solution: • examine $trap object after trap { .. } ok $trap->exit, 0, "exited 0"; like $trap->die, qr{missing args}; • But: • What about stdout, stderr, warnings? Monday, June 25, 12 9
  • 10. • Problem: • What about those outputs? • Solution: • Test::Trap captures those too! like $trap->stdout, qr{usage}; is $trap->stderr, q{}, "quiet errors"; is @{$trap->warn}, 1, "exactly 1 warn"; • But: • What about stubbing or mocking? Monday, June 25, 12 10
  • 11. • Problem: • Want to override some behavior • Solution: • Monkey patching! subtest stub_it => sub { local *Program::some_sub = sub { ... }; trap { Program::run() }; }; • But: • What about stdin? Monday, June 25, 12 11
  • 12. • Problem: • Provide stdin for script • Solution: • Small matter of programming: local *STDIN; open STDIN, "<", (my $S = join(q{})); $$S .= "onentwonthreen"; trap { ... }; $$S .= "fournfiven"; trap { ... }; • But: • What about chdir? Monday, June 25, 12 12
  • 13. • Problem: • chdir has global effect • Solution: • Test::Trap is pluggable! use Test::Trap::mine qw(:cwd); trap { chdir "/tmp"; Program::run() }; • See my blog, or might be core now • But: • Does this really work for all scripts Monday, June 25, 12 13
  • 14. • Problem: • Script might need complex interaction • Maybe can’t edit code into run() • Code might fork • Solution: • Yeah, traditional subprocesses • Perhaps combined with Expect • But: • Test::Trap is amazingly useful! Monday, June 25, 12 14
  • 15. Follow me • Twitter: @merlyn • G+: Randal L. Schwartz • Personal blog: merlyn.posterous.com • http://blogs.perl.org/users/randal_l_schwartz/ • merlyn, realmerlyn, or RandalSchwartz Monday, June 25, 12 15