SlideShare uma empresa Scribd logo
1 de 15
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a Testing scripts

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
zynamics 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 malware
zynamics GmbH
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplained
shannomc
 
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
Michael Girouard
 
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
John Anderson
 
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
arbitrarycode
 

Semelhante 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
 

Mais de Randal Schwartz

Mais 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

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
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
 
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...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - 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