<?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>Forem: dankturtle</title>
    <description>The latest articles on Forem by dankturtle (@abrowncownow).</description>
    <link>https://forem.com/abrowncownow</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%2F898782%2F06d12c2b-1fb6-4a04-a7a7-c87fbd915607.jpeg</url>
      <title>Forem: dankturtle</title>
      <link>https://forem.com/abrowncownow</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abrowncownow"/>
    <language>en</language>
    <item>
      <title>Hello World and Unit Testing with Python</title>
      <dc:creator>dankturtle</dc:creator>
      <pubDate>Wed, 27 Jul 2022 21:02:00 +0000</pubDate>
      <link>https://forem.com/abrowncownow/hello-world-and-unit-testing-with-python-34o</link>
      <guid>https://forem.com/abrowncownow/hello-world-and-unit-testing-with-python-34o</guid>
      <description>&lt;p&gt;Hello community. I've been in the IT field for decade or so and am now dipping my toes into the 'deep-end' of the field. Perhaps I should use 'deeper', as depth may somewhat be a matter of perspective. I expect many of you have a group you can confidently point to and declare them as the real nuts of the bunch.&lt;/p&gt;

&lt;p&gt;I came across &lt;a href="https://www.sitepoint.com/python-unit-testing-unittest-pytest/?utm_source=rss&amp;amp;ref=dailydevbytes.com"&gt;a thorough guide&lt;/a&gt; on testing code using &lt;code&gt;unittest&lt;/code&gt; and &lt;code&gt;pytest&lt;/code&gt;. I've condensed it into concise take-ways. In this article I focus on how to use &lt;code&gt;unittest&lt;/code&gt; and how to understand the sometimes confusing results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Software Testing&lt;/strong&gt; is helpful to identify bugs and inconsistencies in code that can cause errors. The &lt;a href="https://www.istqb.org/"&gt;ISTQB&lt;/a&gt; breaks software testing into 4 levels:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Unit Testing&lt;/strong&gt; - tests specific lines of code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration Testing&lt;/strong&gt; - tests integration between many units&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Systems Testing&lt;/strong&gt; - tests entire system&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Acceptance Testing&lt;/strong&gt; - checks compliance with acceptance criteria&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's critical to note that just because a test passes, doesn't mean that the software is entirely bug-free. (If only it were that easy!)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unit Testing&lt;/strong&gt; verifies correct behavior of a 'unit' which may be a class, function, or method depending on the language. Unit testing is a low-level process which can identify bugs at local level before they propagate to other levels of the software system. Testing many units individually has several benefits: It eases identifying and locating integration problems. Bugs resulting from future modifications can be easier to location (provided good test history and documentation). Repeatedly testing the relationship of unit input and output provides documentation (results) prior to deployment.&lt;/p&gt;

&lt;p&gt;Additional Qualities of a Unit Test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fast (in most cases)&lt;/li&gt;
&lt;li&gt;Isolated - testing of individual units doesn't rely on external processes (like network resources, files, etc)&lt;/li&gt;
&lt;li&gt;Repeatable - Many tests can confirm consistency of results&lt;/li&gt;
&lt;li&gt;Naming - The name of a unit test should clarify the tests purpose&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A common &lt;strong&gt;Organization Strategy&lt;/strong&gt; for writing unit tests is the Arrange, Act, and Assert (AAA) pattern.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrange - set objects and variables&lt;/li&gt;
&lt;li&gt;Act - call the unit being tested (function/class/method)&lt;/li&gt;
&lt;li&gt;Assert - declare expected outcome&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Having a uniform strategy like AAA will ensure your tests are clean and easy to read.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unit Testing in Python&lt;/strong&gt; is primarily performed with &lt;code&gt;unittest&lt;/code&gt; and &lt;code&gt;pytest&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unittest&lt;/code&gt; is included in the standard Python library and contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;test case - tests a single unit&lt;/li&gt;
&lt;li&gt;test suite - group of tests that run together&lt;/li&gt;
&lt;li&gt;test runner - component that handles the execution and results of all test cases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;unittest&lt;/code&gt; is written as methods of a class that subclasses &lt;code&gt;unittest.TestCase&lt;/code&gt; and can use special assertion methods.&lt;/p&gt;

&lt;p&gt;The guide uses the &lt;strong&gt;Example&lt;/strong&gt; of a bank account that is designed to not allow withdraws greater than the current balance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import unittest

class BankAccount:
  def __init__(self, id):
    self.id = id
    self.balance = 0

  def withdraw(self, amount):
    if self.balance &amp;gt;= amount:
      self.balance -= amount
      return True
    return False

  def deposit(self, amount):
    self.balance += amount
    return True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A new test case is made in the same python file by creating class &lt;code&gt;TestBankOperations&lt;/code&gt; that's a subclass of &lt;code&gt;unittest.TestCase&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TestBankOperations(unittest.TestCase):
    def test_insufficient_deposit(self):
      # Arrange
      a = BankAccount(1)
      a.deposit(100)
      # Act
      outcome = a.withdraw(200)
      # Assert
      self.assertFalse(outcome)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the AAA method is used to Arrange (establish variables), Act (call unit to be tested), and Assert (verify outcome). Also note how the test method &lt;code&gt;test_insufficient_deposit&lt;/code&gt; begins with &lt;code&gt;test&lt;/code&gt;. This is important because every test method must start with &lt;code&gt;test&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ideally this test returns the result &lt;code&gt;false&lt;/code&gt;. This is because we asked the test to deposit 100 and try to withdraw 200. To Assert the result, the example uses the special assertion method &lt;code&gt;assertFalse()&lt;/code&gt;.  Put more simply - we expect that depositing 100 and withdrawing 200 will fail. By using the special assertion method, we are declaring that the test will have passed if &lt;code&gt;test_insufficient_deposit&lt;/code&gt; fails.&lt;/p&gt;

&lt;p&gt;We &lt;strong&gt;Run The Test&lt;/strong&gt; by opening the command line and running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m unittest example.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(&lt;code&gt;example.py&lt;/code&gt; being the file containing the source code we are testing.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Results&lt;/strong&gt; should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This indicates that our test passed and the attempt to withdraw in excess of balance failed.&lt;/p&gt;

&lt;p&gt;We can generate a &lt;strong&gt;Failed Test&lt;/strong&gt; for this example by creating another unit test that tries to deposit a negative amount to our account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def test_negative_deposit(self):
    # Arrange
    a = BankAccount(1)
    # Act
    outcome = a.deposit(-100)
    # Assert
    self.assertFalse(outcome)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get more thorough results from the failed test we add &lt;code&gt;-v&lt;/code&gt; (verbose) to &lt;code&gt;unittest&lt;/code&gt; in the command line. We run unit test, which will now run both the insufficient deposit test and the negative deposit test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m unittest -v example.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test_insufficient_deposit (example.TestBankOperations) ... ok
test_negative_deposit (example.TestBankOperations) ... FAIL

======================================================================
FAIL: test_negative_deposit (example.TestBankOperations)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example.py", line 35, in test_negative_deposit
    self.assertFalse(outcome)
AssertionError: True is not false

----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see that our second test failed in line 35. In &lt;code&gt;self.assertFalse(outcome)&lt;/code&gt; we asserted that our test should return false, as we don't want our program accepting negative deposits. However the outcome was true (successful) and therefore our test failed. &lt;/p&gt;

&lt;p&gt;We can use this information to determine that we may need to modify the code if refusing negative deposits is one of the design requirements for our program.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unittest&lt;/code&gt; has &lt;strong&gt;Different Assertion Methods&lt;/strong&gt; based on your needs&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;assertEqual(x,y)&lt;/code&gt; - tests &lt;code&gt;x==y&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assertRaises(exception_type)&lt;/code&gt; - checks if exceptions are raised&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assertIsNone(x)&lt;/code&gt; - tests if &lt;code&gt;x is None&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assertIn(x,y)&lt;/code&gt; - tests if &lt;code&gt;x in y&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To &lt;strong&gt;Expand Functionality&lt;/strong&gt; of unit testing in Python, it is recommended to use the &lt;code&gt;pytest&lt;/code&gt; library (non-native), which allows for more complex testing using less code, supports the native &lt;code&gt;unittest&lt;/code&gt; suites and offers more than 800 external plugins.&lt;/p&gt;

&lt;p&gt;If you have made it this far, thanks for reading my post. I hope I was able to concisely explain and demonstrate the value of &lt;code&gt;unittest&lt;/code&gt; and help some beginners make more sense of somewhat confusing results (like &lt;code&gt;assertfalse&lt;/code&gt; failed true is not false) indicate. Credit for code examples goes to Lorenzo Bonannella in his &lt;a href="https://www.sitepoint.com/python-unit-testing-unittest-pytest/?utm_source=rss&amp;amp;ref=dailydevbytes.com"&gt;very thorough guide&lt;/a&gt; of testing using &lt;code&gt;unittest&lt;/code&gt; and &lt;code&gt;pytest&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>python</category>
    </item>
  </channel>
</rss>
