<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sampriddy</title>
    <description>The latest articles on DEV Community by sampriddy (@sampriddy).</description>
    <link>https://dev.to/sampriddy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F63575%2F4f498988-8f80-423b-95cf-248771bbb634.png</url>
      <title>DEV Community: sampriddy</title>
      <link>https://dev.to/sampriddy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sampriddy"/>
    <language>en</language>
    <item>
      <title>Works as shown: Elixir's doctest macro</title>
      <dc:creator>sampriddy</dc:creator>
      <pubDate>Sat, 12 May 2018 15:57:34 +0000</pubDate>
      <link>https://dev.to/sampriddy/works-as-shown-elixirs-doctest-macro-2gmj</link>
      <guid>https://dev.to/sampriddy/works-as-shown-elixirs-doctest-macro-2gmj</guid>
      <description>&lt;h2&gt;
  
  
  What is doctest?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;doctest&lt;/code&gt; is a macro that searches a specified module for code examples and automatically generates test cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use doctest?
&lt;/h2&gt;

&lt;p&gt;Primarily to ensure the correctness of your module documentation. Doctest examples are also easy to write, can replace simple unit tests, and provide a standard format for specifying code examples and demonstrating your module's API.&lt;/p&gt;

&lt;p&gt;Untested documentation trends toward incorrectness over time and eventually becomes worse than not having documentation at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="c1"&gt;# lib/small_math.ex&lt;/span&gt;
&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;SmallMath&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nv"&gt;@doc&lt;/span&gt; &lt;span class="sd"&gt;"""
  Adds two numbers together.

  ## Examples
    iex&amp;gt; SmallMath.add(1, 2)
    3

    iex&amp;gt; SmallMath.add(5, 7)
    23
  """&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# test/small_math_test.exs&lt;/span&gt;
&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;SmallMathTest&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;ExUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Case&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;async:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
  &lt;span class="n"&gt;doctest&lt;/span&gt; &lt;span class="no"&gt;SmallMath&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mix test

Compiling 1 file (.ex)
.

  1) doctest SmallMath.add/2 (2) (SmallMathTest)
     test/small_math_test.exs:3
     Doctest failed
     code: SmallMath.add(5, 7) === 23
     left: 12
     stacktrace:
       lib/small_math.ex:9: SmallMath (module)

Finished in 0.03 seconds
2 doctests, 1 failure


Randomized with seed 888764
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we see, doctest generated two test cases from our documentation, one of which failed. &lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;doctest&lt;/code&gt; looks for code examples by searching for lines beginning with &lt;code&gt;iex&amp;gt;&lt;/code&gt; and evaluates them as if it were a user sitting at the console. Afterward, a line that specifies a bare value (i.e. does not begin with &lt;code&gt;iex&amp;gt;&lt;/code&gt; or &lt;code&gt;...&amp;gt;&lt;/code&gt;) is interpreted as a result, and a test is generated to assert that the value specified is what a user would see if they had evaluated the last expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="nv"&gt;@doc&lt;/span&gt; &lt;span class="sd"&gt;"""
iex&amp;gt; 5 + 5  # expression
10          # result
"""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Multiline expressions
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;...&amp;gt;&lt;/code&gt; to build a multiline expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="nv"&gt;@doc&lt;/span&gt; &lt;span class="sd"&gt;"""
  iex&amp;gt; add_one_fn = fn(number) -&amp;gt;
  ...&amp;gt;   number + 1
  ...&amp;gt; end
  iex&amp;gt; add_one_fn.(5)
  6
"""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing for exceptions
&lt;/h3&gt;

&lt;p&gt;Writing an example demonstrating an exception is also supported!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="nv"&gt;@doc&lt;/span&gt; &lt;span class="sd"&gt;"""
  iex&amp;gt; 1 + "hello"
  ** (ArithmeticError) bad argument in arithmetic expression
"""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Caveats
&lt;/h2&gt;

&lt;p&gt;Tests generated by &lt;code&gt;doctest&lt;/code&gt; do not run in isolation of each other. Code that generates side effects is not a good match. Binding variables or defining code inside of a doctest also carries the risk that your code will be reused in another test.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
