<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[The Perl Shop - Medium]]></title>
        <description><![CDATA[Corporate blog for The Perl Shop. - Medium]]></description>
        <link>https://medium.com/the-perl-shop?source=rss----df026e1c5b2d---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>The Perl Shop - Medium</title>
            <link>https://medium.com/the-perl-shop?source=rss----df026e1c5b2d---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 02:05:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/the-perl-shop" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Viable is Perl?]]></title>
            <link>https://medium.com/the-perl-shop/how-viable-is-perl-e86b82529df5?source=rss----df026e1c5b2d---4</link>
            <guid isPermaLink="false">https://medium.com/p/e86b82529df5</guid>
            <category><![CDATA[perl]]></category>
            <dc:creator><![CDATA[Tim King]]></dc:creator>
            <pubDate>Sat, 30 Jun 2018 15:57:44 GMT</pubDate>
            <atom:updated>2018-06-30T16:08:22.003Z</atom:updated>
            <content:encoded><![CDATA[<p>A few months ago, John D. Cook wrote about the <a href="https://www.johndcook.com/blog/2018/04/17/unpopular-languages/">viability of unpopular programming languages</a>.</p><p>His story starts with a comment about Perl 6, to which someone replied, “Does anyone actually use Perl 6?” (or words to that effect).</p><p>“My first thought,” he writes, “was, <em>I bet more people use Perl 6 than Haskell, and it’s well known that people use Haskell.</em>”</p><p>What is the relationship between popularity and viability?</p><h3>Does a language need to be popular to be viable?</h3><p>Cook compares the popularity of Perl to Haskell and several other languages:</p><ul><li>“Common Lisp has been around since 1982, and was standardizing a language that had been in development since 1958.”</li><li>“Erlang has been around since 1986.”</li><li>“There is not a huge community devoted specifically to F#, but it shares tooling and libraries with C#…”</li></ul><p>Popularity provides many hands to work on projects and many eyes to address issues with the platform. However, both Lisp and Erlang, he says, “have many of the benefits of popularity…<strong>accumulated over time</strong>” (emphasis mine). And all of these languages would be “safer bets for a production software project than several more popular languages.”</p><p>As it is with art and entertainment, a long-lived cult-classic technology can be more viable than one that is popular in the moment.</p><p>He used the TIOBE index for his stats. I prefer the <a href="http://pypl.github.io/PYPL.html">PYPL index</a>, which uses Google Trends to track the number of searches on the term “<em>&lt;language&gt;</em> tutorial.” They explain in their FAQ:</p><blockquote><em>“C programming” is used much more than “PHP programming,” because PHP does not need the qualifier. </em>Tutorial<em> is a word used frequently by developers learning any new language: it makes a good leading indicator. What is a “python tutorial,” if not a tutorial on the programming language?</em></blockquote><p>So this methodology should measure not how many people are <em>using</em> a given language, but how many are <em>learning</em> it.</p><p>Common Lisp, Erlang, and F# are missing from PYPL, because none of them is popular enough. Perl and Haskell do appear on the index, both at &lt;1% and sinking.</p><p>However, PYPL (like TIOBE) measures popularity based on <em>share</em> of searches, not the <em>volume</em> of them. So if language A becomes bigger because more people are searching for language tutorials in general, language B’s graph line descends, even if its overall volume continues to increase.</p><h3>How popular is Perl?</h3><p>When I’m doing research, I’m usually more interested in the absolute volume. Therefore, I took to Google Trends myself. I downloaded data for a number of programming languages (number of searches per week), smoothed the raw search counts with a Gaussian weighting function, and plotted them on a logarithmic y-axis. The languages I looked up included all the ones in John D. Cook’s article, plus Go, Perl 6, and Dart. I also included the variation “GoLang tutorial” and added it to “Go tutorial.”</p><p>And here’s what I saw:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FPsxQfeqqHT7ZXJnrzJQA.png" /></figure><p>I also noticed some other interesting phenomena:</p><ul><li>Perl is indeed more popular than Haskell, Lisp, Erlang, etc. Or at least more people are looking up Perl tutorials than tutorials on those other languages.</li><li>No one seems to be searching for “Perl 6 tutorial.” Literally, zero searches recorded as far as I can determine. Google must be filtering them out somehow or lumping them together with “Perl tutorial,” even though the two searches return distinctly different results. So Google does seem to know the difference, but Google Trends is still not showing any searches for P6 tutorials, and I don’t believe that <em>no one</em> is searching for them.</li><li>Dart is still a minor player. There are some searches for “Dart tutorial,” but there were so few that I didn’t even include it in the graph.</li><li>There was a noticeable spike in searches for “Go tutorial” for about a month during July 2016. GopherCon 2016 was held July 11th through the 13th. Probably not a mere coincidence.</li><li>There were quite a few searches for “Go tutorial” back in 2004, years before the language was even invented. I’m not sure what those searches were pulling up back then, but I’m pretty sure current searches are talking about the programming language. (This also belies the PYPL’s idea that “What is a ‘go tutorial’ if not a tutorial on the Go programming language?”)</li></ul><h3>Which of these are viable languages?</h3><p>I concur with John D. Cook on Haskell, Lisp, Erlang, and F#.</p><p>GoLang is a viable language, being almost a decade old now, supported by Google, and increasingly used in production projects. It seems to me its growing popularity is a symptom (not a cause) of its viability.</p><p>The most interesting manifestation of the popularity-viability conundrum, however, is Dart. Despite <a href="https://www.youtube.com/watch?v=cDMMF_b7Kbs">Randal Schwartz’s enthusiasm for Dart</a>, the language is still seeking its niche. It’s been around for 7 years, but it has not yet reached the adoption chasm, much less crossed it. It may have found a “killer app” in Flutter (which is a truly cool framework for mobile apps, and <a href="https://www.youtube.com/watch?v=fxFhBw6pbH8">Randal Schwartz’s recent lightning talk on Flutter</a> frankly didn’t do it justice). But whether Dart will make it big is still yet to be seen.</p><p>Even so, despite its lack of popularity <em>and</em> its newness, <a href="https://youtu.be/ekBD-_jRjds?t=48s">Dart is a viable platform for production projects</a>.</p><p>Yes, people actually do use Dart for production projects, mostly in web and mobile development.</p><p>If Dart is viable, then how much more so Perl?</p><p><em>This post originally appeared on The Perl Shop blog as </em><a href="http://blog.theperlshop.com/2018/06/28/how-viable-is-perl/?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=cross-post"><em>“How Viable is Perl?”</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e86b82529df5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-perl-shop/how-viable-is-perl-e86b82529df5">How Viable is Perl?</a> was originally published in <a href="https://medium.com/the-perl-shop">The Perl Shop</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Long Import Lists in Perl (and available strategies for managing them)]]></title>
            <link>https://medium.com/the-perl-shop/long-import-lists-in-perl-and-available-strategies-for-managing-them-2da6a96b66fd?source=rss----df026e1c5b2d---4</link>
            <guid isPermaLink="false">https://medium.com/p/2da6a96b66fd</guid>
            <category><![CDATA[perl]]></category>
            <dc:creator><![CDATA[Tim King]]></dc:creator>
            <pubDate>Wed, 20 Jun 2018 10:10:40 GMT</pubDate>
            <atom:updated>2018-06-20T10:13:17.832Z</atom:updated>
            <content:encoded><![CDATA[<p>I ran across this the other day while writing some sample code for the next chapter of <a href="http://www.theperlshop.com/in/testing-modern-perl?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=cross-post"><em>Testing Strategies for Modern Perl</em></a>.</p><p><strong>How can we use long lists of symbols from an imported package and still keep the code readable?</strong></p><p>I usually prefer use statements of the form:</p><pre>use My::Module qw(symbol1 symbol2 symbol3);</pre><p>Except for specially understood modules, like Moose and Test::More, I don’t like to just import everything. Rather I like to explicitly call out only the specific symbols I need.</p><p>But what if you need to:</p><pre>use My::Module qw(<br>    symbol1 symbol2 symbol3 symbol4 symbol5 etc and so many symbols<br>    that it takes up several lines all the time in every package<br>    that uses it<br>);</pre><p>There are a few alternative approaches.</p><h3>Just import everything</h3><p>We could just fall back on importing everything.</p><p>So… Five years from now, I’m going to revisit that code, and I’ll see:</p><pre>wiggle_foo_gadget(WIGGLE_WOBBLE);</pre><p>And my first question is going to be, “Which of those 17 modules does that come from?!” And then I’ll need to grep through the entire codebase to find it.</p><p>That’s why I like to call out the specific symbols being imported, and that’s our standard practice here at The Perl Shop. If the symbol is explicitly imported, a simple search through the current module will find it, and it will be clear where the symbol came from.</p><p>Additionally, when we import everything, we have zero control over what symbols are imported. If a later version of a used module exports more symbols, then they’ll automatically get imported in our package, whether we want them there or not. This could result in name clashes and action-at-a-distance bugs down the line.</p><p>So alternative option #2…</p><h3>Don’t import anything</h3><p>That is, we can just use the full package name. So:</p><pre>use My::Module ();</pre><pre>do_something_with($My::Module::scalar_var);<br>do_something_else_with(@My::Module::array_var)<br>My::Module::do_the_hustle();</pre><p>This is generally my preferred alternative. Additionally, reading My::Module::do_the_hustle(), having the module cited adds context and can make the code easier to read.</p><p>But what if My::Module is actually more like My::External::Module::With::Several::Levels? Copying and pasting that monstrosity everywhere, that’s going to get pretty confusing pretty fast.</p><p>And in my sample code, this was indeed what I was facing. So not a viable alternative.</p><h3>Use import tags</h3><p>This is a well-established idiom, implemented directly by Exporter.</p><p>With a tag, the use line would look something like this:</p><pre>use My::Really::Long::Module::Name qw(:some_symbols);</pre><p>This is much more concise, and it gives us at least some level of control over what is being imported. But it still doesn’t make clear where the symbols came from.</p><p>This is the option I chose in my sample code. Mostly I did so for the reasons above. In production code, I would probably have opted for the very last of the options below (“Lexical aliasing”). But in this case, it was sample code for a testing book, and I really didn’t want to take a section of the book to explain that non-standard idiom, because it had nothing to do with testing.</p><h3>Export a hash of symbols</h3><p>This is very nonstandard, but one thought that occurs to me is that one could export read-only hashes of symbols.</p><p>So in <strong>My/Really/Long/Module/Name.pm</strong>:</p><pre>package My::Really::Long::Module::Name;</pre><pre>use Readonly;</pre><pre>Readonly::Hash our %some_symbols =&gt; (<br>    symbol1 =&gt; \&amp;symbol1,<br>    symbol2 =&gt; \&amp;symbol2,<br>    symbol3 =&gt; \&amp;symbol3,<br>    # ... and so forth<br>);</pre><p>Then we can:</p><pre>use My::Really::Long::Module::Name qw(%some_symbols);</pre><pre>$some_symbols{symbol1}();<br>$some_symbols{symbol2}();<br>$some_symbols{symbol3}();</pre><p>That’s a little awkward, but it is succinct (at least in the using module) and does identify the symbol source.</p><p>But it doesn’t exactly work with variables. That is, as soon as you put a reference to anything in a read-only data structure, the anything itself becomes read-only.</p><p>There’s a better alternative…</p><h3>Alias the used package</h3><p>That is, use My::Really::Long::Module::FooBar and then refer to it as simply FooBar.</p><p>We can accomplish this with <a href="https://metacpan.org/pod/Package::Alias">Package::Alias</a>:</p><pre>use Package::Alias FooBar =&gt; &#39;My::Really::Long::Module::FooBar&#39;;</pre><pre>FooBar::make_it_rain();<br>say $FooBar::is_raining;</pre><p>The Package::Alias line above is actually syntactic sugar for:</p><pre>BEGIN {<br>    use My::Really::Long::Module::FooBar;<br>    *{FooBar::} = \*{My::Really::Long::Module::FooBar::};<br>}</pre><p>The disadvantage here is that the alias is global, not lexical. That is, if one module aliases FooBar, then another module can’t also alias FooBar (whether or not they’re aliasing to the same target). Package::Alias will warn if you attempt that and then ignore the second and subsequent attempts.</p><p>(Note also that Package::Alias will load the target package into the caller’s namespace <em>only</em> if it hasn’t been used previously. And it actually does use the default use, which imports all available symbols into the caller’s namespace—but only if no other package has formerly loaded it. For this reason, this functionality is only appropriate for packages that do not export symbols. Otherwise, you need to explicitly use My::Module () before using Package::Alias.)</p><h3>Lexical aliasing</h3><p>What I’d really like is to alias the target in lexical scope.</p><p><a href="https://metacpan.org/pod/namespace::alias">namespace::alias</a> claimed to do that. It uses Perl internals to accomplish its black magic. Unfortunately, there are several critical documented issues, and it has not successfully built since Perl 5.18. It was last released in 2012. So not an option.</p><p><a href="https://metacpan.org/pod/aliased">aliased</a> does something similar. It creates a short-named subroutine that returns the long name of a target package. It also has magic to figure out what the subroutine should be named and other features. I haven’t tried the module, but it looks like quite an elegant design: simple and powerful. Unfortunately, it really only works for object-oriented code. So a tool for the toolbox, but not applicable to this blog post.</p><p>The best I was able to come up with was this:</p><pre>use My::Really::Long::Module::FooBar ();</pre><pre>my $FooBar = \%My::Really::Long::Module::FooBar::;</pre><pre>$FooBar-&gt;{do_something}();<br>$FooBar-&gt;{do_something_else}();<br>my $is_done = $FooBar-&gt;{is_it_done}();</pre><pre>$FooBar-&gt;{scalar_var}-&gt;$* = &#39;foo&#39;;</pre><pre>push $FooBar-&gt;{array_var}-&gt;@*, qw(bar baz qux quux);</pre><pre>$FooBar-&gt;{hash_var}{spam} = &#39;eggs&#39;;</pre><p>This is slightly awkward, but it works. And it’s a nonstandard idiom, but it’s not so confusing that a competent Perl programmer can’t quickly figure out what’s going on. And maybe if we used it more, it would catch on.</p><p>This is an idiom I will keep in mind for future projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2da6a96b66fd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-perl-shop/long-import-lists-in-perl-and-available-strategies-for-managing-them-2da6a96b66fd">Long Import Lists in Perl (and available strategies for managing them)</a> was originally published in <a href="https://medium.com/the-perl-shop">The Perl Shop</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Programmers Use the Test Hierarchy Antipattern]]></title>
            <link>https://medium.com/the-perl-shop/why-programmers-use-the-test-hierarchy-antipattern-9fa05a5b6b00?source=rss----df026e1c5b2d---4</link>
            <guid isPermaLink="false">https://medium.com/p/9fa05a5b6b00</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[test-class]]></category>
            <category><![CDATA[automated-testing]]></category>
            <category><![CDATA[perl]]></category>
            <dc:creator><![CDATA[Tim King]]></dc:creator>
            <pubDate>Wed, 06 Jun 2018 03:31:01 GMT</pubDate>
            <atom:updated>2018-06-06T03:31:01.435Z</atom:updated>
            <content:encoded><![CDATA[<p><em>The </em><a href="https://medium.com/p/2f1aaf51d1a8"><em>first part of this series</em></a><em> described Test Hierarchy, a hierarchy of test classes that mirrors the classes under test, and explained why it’s an antipattern. </em><a href="https://medium.com/p/fb027b962a3"><em>Part two</em></a><em> explored what makes a good unit test and why Test Hierarchy does not. This third and final post reflects on why programmers use Test Hierarchy and why these reasons aren’t persuasive.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZDJbvTb6LOHSdGF0FDNgGw.png" /></figure><p>I think there are a couple reasons why programmers use Test Hierarchy.</p><p>Test Hierarchy may appear to “just make sense” at first blush. After all, you have a hierarchy of classes under test — superclasses and subclasses — and you have a collection of test classes. It seems very symmetrical to have the test classes mirror the classes under test.</p><p>However, there’s no <em>design</em> justification for the test classes to be arranged in a parallel hierarchy. The easiest way to see this is to consider what happens when developers get tired of Test Hierarchy. What do they do? They drop back to <em>procedural</em> tests, with <em>no inheritance at all.</em> If you don’t need Test Hierarchy to test object-oriented code using procedural tests, why do you need it when using Test::Class? Answer: You don’t.</p><p><strong>Test inheritance should only be used to meet the needs of the tests, not the needs of the code under test.</strong></p><p>This usually means that if we have test superclasses, they specifically contain shared setup and teardown code or test utility functions. See <a href="http://xunitpatterns.com/Testcase%2520Superclass.html"><em>the Testcase Superclass pattern</em></a>, by which a test class can inherit common functionality from an abstract test superclass. Using this pattern, the test hierarchy is organized in order to share common code across the entire project’s tests or an entire subsystem’s tests.</p><p>(But use SharedTestModule qw(shared_function) is still preferred over inheritance, because it more explicitly states what is being shared and where.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J-R8gn3oeMNNFAOxY4B09A.png" /></figure><p>I’ve also seen programmers appeal to the <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle"><em>Liskov Substitution Principle</em></a>. This is the idea that if Bat is a subclass of Mammal, then any code that requires a Mammal can be handed a Bat without ill effects. Barbara Liskov and Jeanette Wing formally defined it like this:</p><blockquote><em>Let </em>ϕ(x)<em> be a property provable about objects </em>x<em> of type T. Then </em>ϕ(y)<em> should be true for objects </em>y<em> of type S where S is a subtype of T. (</em><a href="http://reports-archive.adm.cs.cmu.edu/anon/1999/CMU-CS-99-156.pdf">“Behavioural Subtyping Using Invariants and Constraints.”</a><em> Barbara Liskov, Jeanette Wing. CMU-CS-99–156. MIT Lab, July 1999.)</em></blockquote><p><strong>In other words, a subclass adheres to the same interface contract as its superclasses.</strong></p><p>Some programmers will say that if Bat is a subclass of Mammal, then a Bat can do anything that a Mammal can. In other words, a Bat “is a” Mammal. Therefore, it directly follows that BatTest should test all the Mammal behaviors that Bat inherits.</p><p>No, it doesn’t, and no, it shouldn’t.</p><p>To understand why, consider a couple simple cases.</p><ul><li>If a Mammal has a care_for_young() method, then every Batmust also be able to care_for_young(). This does <em>not</em> mean that the way bats care for their young is exactly the same as every other mammal. In fact, it’s distinctly different, because baby bats have needs that are distinct from the needs of other baby mammals. In fact, Mammal::care_for_young() may even be an abstract method that dies with a “must be implemented in subclass” error.</li><li>Similarly, every Animal can move(). That means every Mammal also can move(), because Mammal is a subclass of Animal. By extension every Bat can also move(), because Bat is a subclass of Mammal. Now explain to me how the way a bat moves is identical to the way a sloth moves or the way a tarantula moves. It isn’t.</li></ul><p><strong>A subclass adheres to the same <em>interface contract</em> as its superclasses. It does not necessarily implement identical behaviors.</strong></p><p>Therefore, just because a Bat “is a” Mammal, that doesn’t mean that a BatTest “is a” MammalTest. Actually, no, a BatTest is not a MammalTest. Not even close. Both BatTest and MammalTest are just tests. Or they might, at most, be derived from OrganismTestabstract class which contains helper methods to set up and manage <a href="https://github.com/junit-team/junit4/wiki/test-fixtures">test fixtures</a> common to all organisms.</p><p>Test::Class can test anything straight Test::More can, and vice-versa. The power in Test::Class is not as legend says in testing object-oriented code. The power Test::Class brings is its ability to collect related test methods together, run them independently, and inherit setup and teardown. (I’ll explore more of these details in <a href="http://www.theperlshop.com/in/testing-modern-perl?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=cross-post">Testing Strategies for Modern Perl</a>.) Each test class should be derived directly from Test::Class or from an <em>abstract</em> subclass thereof. Never inherit test methods. Just don’t do it.</p><p>Peace, love, and may all your TAP output turn green…</p><p><em>This post originally appeared on The Perl Shop blog as </em><a href="http://blog.theperlshop.com/2018/06/05/why-programmers-use-the-test-hierarchy-antipattern/?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=cross-post"><em>“Why Programmers Use the Test Hierarchy Antipattern.”</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fa05a5b6b00" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-perl-shop/why-programmers-use-the-test-hierarchy-antipattern-9fa05a5b6b00">Why Programmers Use the Test Hierarchy Antipattern</a> was originally published in <a href="https://medium.com/the-perl-shop">The Perl Shop</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test Hierarchy Produces Poor Unit Tests]]></title>
            <link>https://medium.com/the-perl-shop/test-hierarchy-produces-poor-unit-tests-fb027b962a3?source=rss----df026e1c5b2d---4</link>
            <guid isPermaLink="false">https://medium.com/p/fb027b962a3</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[test-class]]></category>
            <category><![CDATA[perl]]></category>
            <category><![CDATA[automated-testing]]></category>
            <dc:creator><![CDATA[Tim King]]></dc:creator>
            <pubDate>Mon, 04 Jun 2018 02:27:35 GMT</pubDate>
            <atom:updated>2018-06-05T04:01:20.897Z</atom:updated>
            <content:encoded><![CDATA[<p><em>The </em><a href="https://medium.com/p/2f1aaf51d1a8"><em>first part of this series</em></a><em> described Test Hierarchy, a hierarchy of test classes that mirrors the classes under test, and explained why it’s an antipattern. For how common it is, this practice doesn’t even produce good unit tests.</em></p><p>A unit test, by definition, tests a unit of software, no more, no less. On the one hand, we have <strong>unit tests</strong>, which test a single module or class. On the other hand, we have <strong>integration tests</strong>, which test how multiple modules or classes work together. We want each unit test to poke and prod only the class that it tests. We want each subsystem integration test to test a natural subsystem, e.g., the data-export subsystem. We want our system tests to test the <em>whole</em> system. And we don’t want any test to be affected by any other other units, subsystems, or systems.</p><blockquote><em>When our software depends on other software that may change over time, our tests may suddenly start failing because the behavior of the other software has changed. This problem, which is called </em>Context Sensitivity,<em> is a form of </em>Fragile Test<em>…</em></blockquote><blockquote><em>Whatever application, component, class, or method we are testing, we should strive to isolate it as much as possible from all other parts of the software that we choose not to test. This isolation of elements allows us to </em>Test Concerns Separately<em> and allows us to </em>Keep Tests Independent<em> of one another. It also helps us create a </em>Robust Test<em> by reducing the likelihood of </em>Context Sensitivity<em> caused by too much coupling between our SUT [system under test] and the software that surrounds it. (</em><a href="https://www.safaribooksonline.com/library/view/xunit-test-patterns/9780131495050/ch05.html"><em>xUnit Test Patterns: Refactoring Test Code.</em></a><em> Gerard Meszaros. Addison-Wesley Professional, 2007.)</em></blockquote><p>If terms like <em>Context Sensitivity</em> and <em>Fragile Test</em> feel familiar, it’s not just a coincidence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J-R8gn3oeMNNFAOxY4B09A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZDJbvTb6LOHSdGF0FDNgGw.png" /></figure><p><strong>Test Hierarchy produces tests that purport to be unit tests but that don’t actually test isolated units.</strong></p><p>Let’s say we have a Bat class that is a subclass of Mammal. If the tests use Test Hierarchy, then BatTest not only tests the Bat code, but also the superclass Mammalcode <em>and</em> its superclass Animal.</p><p>This seems to make some intuitive sense, because after all, Bat can do all the things that Mammal and Animal can do, all the methods that it inherits from those classes. But this intuition misses an important distinction. The <em>unit</em> is whatever code is in the Bat.pm module, not whatever the Batclass can do.¹</p><p>When a Bat unit test fails, it should indicate that we made a mistake in Bat.pm, not any other module.</p><p>Our bad BatTest doesn’t just test the code in Bat.pm, but also the code in Mammal.pm and Animal.pm. This makes it an integration test (not a unit test), because it doesn’t just test its own module but other modules as well.</p><p>And it’s an integration test we don’t need. Generally, we write integration tests that exercise some system feature, like “export Foo data in CSV format.” This might involve setting up the Foo data fixture, invoking the appropriate export feature, then validating the CSV file that it generates. But we don’t need module tests that invoke low-level methods on other modules.</p><p><strong>In fact, who said that there’s only one test per class?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sd6TQQkKevdei72W-j2Shw.png" /></figure><p>Here at The Perl Shop, we generally create a separate test module <em>per method</em> or feature. So we’d create move.t, eat.t, and breathe.t, each of which tests a different Animal method. This way, we can group together tests by class method, and easily self-document which tests correspond with which feature.²</p><p>We also inline our test classes in our .t scripts, which keeps the test code close to the test script and cuts in half the number of files we need to maintain. And makes it impossible to subclass them.</p><p>We’ve had great success with these practices, and they’re fundamentally incompatible with Test Hierarchy.</p><p>You might have also noticed this in <a href="http://www.theperlshop.com/in/testing-modern-perl?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=cross-post">Testing Strategies for Modern Perl</a>. In chapter 2, we create a TicTacToe::BusinessLogic::Game class, which is tested by new.t, board.t, and move.t.</p><p>In <a href="https://medium.com/p/9fa05a5b6b00">the concluding post</a>, I’ll reflect on some of the reasons developers use Test Hierarchy, and why these reasons don’t stack up.</p><p>Peace, love, and may all your TAP output turn green…</p><p><em>This post originally appeared on The Perl Shop blog as </em><a href="http://blog.theperlshop.com/2018/05/29/test-hierarchy-produces-poor-unit-tests/?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=cross-post"><em>“Test Hierarchy Produces Poor Unit Tests.”</em></a></p><p>[1] Formally, Bat.pm doesn’t directly define the Bat class. Rather, it defines an implicit “subclass mixin”—all the methods and attributes in Bat.pm that are added to (“<a href="https://www.dartlang.org/articles/language/mixins"><em>composed with</em></a>“) its superclass Mammal. The object system, then, composes the Bat mixin with Mammal to create the full Bat class. Similarly, Mammal.pm conceptually defines a Mammal mixin that is composed with its superclass Animal in order to create the Mammal class.</p><p>[2] Another alternative is to have a separate test class per <a href="https://github.com/junit-team/junit4/wiki/test-fixtures">fixture</a>, which is useful if different test methods have different fixture requirements.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb027b962a3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-perl-shop/test-hierarchy-produces-poor-unit-tests-fb027b962a3">Test Hierarchy Produces Poor Unit Tests</a> was originally published in <a href="https://medium.com/the-perl-shop">The Perl Shop</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test::Class Hierarchy Is an Antipattern]]></title>
            <link>https://medium.com/the-perl-shop/test-class-hierarchy-is-an-antipattern-2f1aaf51d1a8?source=rss----df026e1c5b2d---4</link>
            <guid isPermaLink="false">https://medium.com/p/2f1aaf51d1a8</guid>
            <category><![CDATA[automated-testing]]></category>
            <category><![CDATA[test-class]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[perl]]></category>
            <category><![CDATA[unit-testing]]></category>
            <dc:creator><![CDATA[Tim King]]></dc:creator>
            <pubDate>Sun, 27 May 2018 03:58:31 GMT</pubDate>
            <atom:updated>2018-06-05T03:56:41.222Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Test::Class is particularly good at testing object-oriented code, or so it is said. You can create a hierarchy of test classes that mirrors the hierarchy of classes under test. But this pattern, common in Perl projects, is conspicuously missing from the rest of the xUnit world, and with good reason.</em></p><p>We’ve all heard of it.</p><p>Our project has a class Animal that implements method move() (because all animals can move). This class has a test class AnimalTest that derives from Test::Class and has a test for $animal-&gt;move().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J-R8gn3oeMNNFAOxY4B09A.png" /></figure><p>So far so good.</p><p>Our project also has a class Bat that derives from Mammal, which derives from Animal, and implements method fly(). So we create a test class BatTest that derives from MammalTest, which in turn derives from AnimalTest, and has a test for $bat-&gt;fly(). That means that BatTest not only exercises all the behavior of Bat but also of Mammal and Animal, because it inherits all the tests in its ancestors.</p><p>Wow! What a cool feature! We get all that testing functionality essentially for free by inheritance!</p><p>And if the foregoing description sounded confusing, just imagine how good it’s going to get as we extend the object hierarchy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZDJbvTb6LOHSdGF0FDNgGw.png" /></figure><p>Repeat for umpteen different classes.</p><p><strong>This arrangement of test classes is what I mean by </strong><strong>Test::Class Hierarchy, or more generally, Test Hierarchy.</strong></p><p>Multiple prominent sources in the Perl community recommend Test Hierarchy.</p><ul><li>This advice appears <a href="https://www.safaribooksonline.com/library/view/perl-testing-a/0596100922/ch08s03.html">in Perl Testing: A Developer’s Notebook</a>, by Ian Langworth and chromatic (published 2005).</li><li>Curtis “Ovid” Poe recommends it <a href="http://www.modernperlbooks.com/mt/2009/03/reusing-test-code-with-testclass.html">on the Modern Perl site</a> (2009), and then reiterates that advice <a href="https://www.safaribooksonline.com/library/view/beginning-perl/9781118235638/xhtml/Chapter14.html">in Beginning Perl</a> (published 2012).</li></ul><p>And most notably, when I ask a roomful of Perl developers about their experiences with Test::Class, I’m sure to hear at least one person complain about “the rabbit hole of inheritance,” as <a href="https://github.com/jjn1056">jnap</a> once put it in a conversation. Of course, not every project misuses Test::Class and its support for inheritance, but as he noted, “I’ve just seen it so wildly abused.” (And to be fair, this is not the only abuse of Test::Class, but it’s the pattern I’m examining at the moment.)</p><ul><li><strong>This makes fragile, overlapping tests.</strong> When we inherit test methods in this way, we end up with test action at a distance. That is, each test class includes tests that are defined in its superclasses, which are <em>completely different modules.</em> A change to any of the superclasses can produce failures in any and all of the subclass tests.</li><li><strong>These tests are obscure.</strong> We can’t know by looking at the test module what functionality it’s testing, at least not without following the inheritance hierarchy all the way to the top.</li><li><strong>And they’re slow to boot.</strong> The test suite takes exponentially longer to run than it needs to, because the same tests are being run over and over again in each subclass.</li></ul><p>In the words of Alyssa Mastromonaco (or maybe her publisher): <a href="https://www.npr.org/2017/04/18/524493295/i-basically-ran-on-adrenaline-a-staffer-remembers-obamas-white-house">Who thought this was a good idea?</a></p><p><strong>Enough Perl projects use Test Hierarchy that it pops up in criticisms of </strong><strong>Test::Classitself, and the negative effects are a significant point when they do.</strong></p><p>It bears noting, however, that this practice is strongly discouraged in the rest of the programming world. It’s so rare, in fact, that Gerard Meszaros doesn’t even mention it in his book <a href="https://www.safaribooksonline.com/library/view/xunit-test-patterns/9780131495050/">xUnit Test Patterns</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sd6TQQkKevdei72W-j2Shw.png" /></figure><p>Rather, the recommended practice is to inherit our test classes directly from Test::Class (or possibly from a project-or subsystem-specific test base class—but that’s a different post). In general, we use as little hierarchy as possible, and whatever hierarchy we do use is organized according to the needs of the tests, not the needs of the system under test. And we never inherit test methods (although we may inherit setup and teardown code).</p><p><strong>In summary, we write independent test classes, and we never inherit test methods.</strong></p><p>That qualifies Test Hierarchy as a Perl <a href="https://en.wikipedia.org/wiki/Anti-pattern%23Definition"><em>antipattern</em></a>:</p><ol><li>It’s a commonly used structure that despite initially appearing to be appropriate and effective, has more bad consequences than good; and</li><li>Another solution exists that is documented, repeatable, and proven to be effective.</li></ol><p>In <a href="https://medium.com/p/fb027b962a3">the next post</a>, I talk about why Test Hierarchy does not even make good unit tests.</p><p>Peace, love, and may all your TAP output turn green…</p><p><em>This post originally appeared on The Perl Shop blog as </em><a href="http://blog.theperlshop.com/2018/05/15/testclass-hierarchy-is-an-antipattern/?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=cross-post"><em>“</em>Test::Class Hierarchy Is an Antipattern.”</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f1aaf51d1a8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-perl-shop/test-class-hierarchy-is-an-antipattern-2f1aaf51d1a8">Test::Class Hierarchy Is an Antipattern</a> was originally published in <a href="https://medium.com/the-perl-shop">The Perl Shop</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>