It seems that Javascript challenges are more frequent at work. Thursday afternoon when we got back from lunch, my coworker was testing himself in the You Can’t Javascript Under Pressure challenges. As great coworkers, we (by that I mean, me) decided to help out and put more pressure on our fellow coworker, without even being asked to! One of the challenges that came up during the test was a function ArraySum() which accepts a list (array) of values and you must total all the integers within the array. The array could contain strings, numbers, booleans, lists of strings, numbers, booleans, etc. Regardless of the input, you must total all the integers within the array. After seeing that challenge (and helping with the correct answer), I thought it might be good practice for another blog post.

Practice makes Perfect

Before jumping into more testing and programming, I want to stop and think about my methodology. While re-reading Chapter 3 of Test-Driven Web Development with Python on Friday with a coworker, I realized that almost every line of code that was written in the chapter to satisfy the test, had a test case as a reason to write that code. After seeing this and thinking about my previous blog post, I realized I didn’t follow the TDD methodology as closely as I couldshould have. According to the author, Harry Percival, TDD is a discipline, it even says so in chapter 4.

TDD is a discipline, and that means it’s not something that comes naturally; because many of the payoffs aren’t immediate but only come in the longer term, you have to force yourself to do it in the moment.

To cut myself some slack, TDD is something you have to practice. Yes, I’m talking about practice. I’m sure there are a bunch of young grasshoper learning quotes I could state right now, but lets get on to testing.

Stricter Test Cases

Last time we started by importing our function and writing a basic test case to test the return type. Let’s do that again.

import unittest
from arraysum import ArraySum

class ArraySumTests(unittest.TestCase):
    def setUp(self):
        pass

    def test_for_int(self):
        result = ArraySum([1, 2, 3, 4, 5,])
        self.assertTrue(type(result) is int, "Result is not integer")

if __name__ == '__main__':
    unittest.main()

Looks good to me, lets run it!

Traceback (most recent call last):
File "tests.py", line 2, in <module>
  from arraysum import ArraySum
  ImportError: No module named arraysum

Our first error! Lets correct it, but remember, minimal amount of code! In fact, we won’t right any code at all.

caster:arraysum/ $ touch arraysum.py
{% endhighlight bash %} 

Let's re-run our test.

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>Traceback (most recent call last):
</span></span><span style="display:flex;"><span>File <span style="color:#e6db74">&#34;tests.py&#34;</span>, line <span style="color:#ae81ff">2</span>, <span style="color:#f92672">in</span> <span style="color:#f92672">&lt;</span>module<span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">from</span> arraysum <span style="color:#f92672">import</span> ArraySum
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">ImportError</span>: cannot <span style="color:#f92672">import</span> name ArraySum</span></span></code></pre></div>

Yay new error! This time it can't find our function, `ArraySum`, lets (minimally) create that in our new `arraysum.py` file.

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ArraySum</span>(int_list):
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">pass</span></span></span></code></pre></div>

Okay, let's see what we get now.
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>F
</span></span><span style="display:flex;"><span><span style="color:#f92672">======================================================================</span>
</span></span><span style="display:flex;"><span>FAIL: test_for_int (__main__<span style="color:#f92672">.</span>ArraySumTests)
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Traceback (most recent call last):
</span></span><span style="display:flex;"><span>  File <span style="color:#e6db74">&#34;tests.py&#34;</span>, line <span style="color:#ae81ff">10</span>, <span style="color:#f92672">in</span> test_for_int
</span></span><span style="display:flex;"><span>  self<span style="color:#f92672">.</span>assertTrue(type(result) <span style="color:#f92672">is</span> int, <span style="color:#e6db74">&#34;Result is not integer&#34;</span>)
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">AssertionError</span>: Result <span style="color:#f92672">is</span> <span style="color:#f92672">not</span> integer
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">1</span> test <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>FAILED (failures<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>)</span></span></code></pre></div>

<del>More</del>MOAR errors!! This time our test is failing because we're not returning an integer. Let's fix that by modifying our return statement.

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ArraySum</span>(int_list):
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span></span></span></code></pre></div>

Alright, lets re-run our test.

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">1</span> test <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>OK</span></span></code></pre></div>

Woohoo! Our first test passed. According to TDD, we can now write some more tests. Let's make this one a little more interesting.
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">import</span> unittest
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> arraysum <span style="color:#f92672">import</span> ArraySum
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ArraySumTests</span>(unittest<span style="color:#f92672">.</span>TestCase):
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">setUp</span>(self):
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">pass</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_for_int</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>,])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(type(result) <span style="color:#f92672">is</span> int, <span style="color:#e6db74">&#34;Result is not integer&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_flat_list</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">15</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
</span></span><span style="display:flex;"><span>    unittest<span style="color:#f92672">.</span>main()</span></span></code></pre></div>

We've now added a new test, `test_flat_list` to test a simple list of integers and ensure the sum is correct. Back to running tests...
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>F<span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">======================================================================</span>
</span></span><span style="display:flex;"><span>FAIL: test_flat_list (__main__<span style="color:#f92672">.</span>ArraySumTests)
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Traceback (most recent call last):
</span></span><span style="display:flex;"><span>File <span style="color:#e6db74">&#34;tests.py&#34;</span>, line <span style="color:#ae81ff">14</span>, <span style="color:#f92672">in</span> test_flat_list
</span></span><span style="display:flex;"><span>  self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">15</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">AssertionError</span>: Incorrect sum
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">2</span> tests <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>FAILED (failures<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>)</span></span></code></pre></div>

The obvious problem here is that our function is returning 0 and not doing any sort of summation. According to TDD, we want to write the minimal amount of code, Python, being the beautiful language that it is, provides us with a simple `sum` function which operates on iterables. Allowing us to very easily pass our `test_with_flat_list` test.

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ArraySum</span>(int_list):
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> sum(int_list)</span></span></code></pre></div>

Running our test again will show that we were able to successfully pass it.
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">..</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">2</span> tests <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>OK</span></span></code></pre></div>

We can continue writing more test cases, so lets make it a little more interesting. We'll introduce some non-integer values into the list, which will create errors for our `sum` function in a test case called `test_complex_list`.
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">import</span> unittest
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> arraysum <span style="color:#f92672">import</span> ArraySum
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ArraySumTests</span>(unittest<span style="color:#f92672">.</span>TestCase):
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">setUp</span>(self):
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">pass</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_for_int</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>,])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(type(result) <span style="color:#f92672">is</span> int, <span style="color:#e6db74">&#34;Result is not integer&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_flat_list</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">15</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_complex_list</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#e6db74">&#34;hello&#34;</span>])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">6</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
</span></span><span style="display:flex;"><span>    unittest<span style="color:#f92672">.</span>main()</span></span></code></pre></div>

Running our tests, returns the results:

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>E<span style="color:#f92672">..</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">======================================================================</span>
</span></span><span style="display:flex;"><span>ERROR: test_complex_list (__main__<span style="color:#f92672">.</span>ArraySumTests)
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Traceback (most recent call last):
</span></span><span style="display:flex;"><span>  File <span style="color:#e6db74">&#34;tests.py&#34;</span>, line <span style="color:#ae81ff">17</span>, <span style="color:#f92672">in</span> test_complex_list
</span></span><span style="display:flex;"><span>  result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#e6db74">&#34;hello&#34;</span>])
</span></span><span style="display:flex;"><span>    File <span style="color:#e6db74">&#34;/home/caster/Development/arraysum/arraysum.py&#34;</span>, line <span style="color:#ae81ff">2</span>, <span style="color:#f92672">in</span> ArraySum
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> sum(int_list)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">TypeError</span>: unsupported operand type(s) <span style="color:#66d9ef">for</span> <span style="color:#f92672">+</span>: <span style="color:#e6db74">&#39;int&#39;</span> <span style="color:#f92672">and</span> <span style="color:#e6db74">&#39;str&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">3</span> tests <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>FAILED (errors<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>)</span></span></code></pre></div>

Just as we expected! Let's make some corrections to our `ArraySum` function.
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ArraySum</span>(int_list):
</span></span><span style="display:flex;"><span>    sum <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> x <span style="color:#f92672">in</span> int_list:
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> type(x) <span style="color:#f92672">is</span> int:
</span></span><span style="display:flex;"><span>            sum <span style="color:#f92672">+=</span> x
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> sum</span></span></code></pre></div>

After making our fix, we'll re-run our test and...
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span> <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">3</span> tests <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>OK</span></span></code></pre></div>

Looks good! Once again, passed test cases means we can write more tests, so lets give it a go one last time. We'll create a new test case, `test_complex_nested_list`, where we'll introduce a nested list into our previous complex list. A nested list within our function also brings in the sense of recursion! We'll want to take in account what we have, where we want to go, and if we've done something like this before (which we have). Currently we're iterating through all the values and if they're integers we'll add them to our `sum` variable. We're throwing away that is anything but an `int`. We know this is wrong though since the function must also include the values within nested lists, hence the embarrassingly recursive algorithm.

We know that we're going to have to iterate through the values in our list, so we understand that, but lets look at our cases:

* Value is an `int`: Add to sum
* Value is a `list`: Add the total of the list to sum
* Value is neither: Ignore

Now that we have an idea of what we want to do, lets write our last test case.
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">import</span> unittest
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> arraysum <span style="color:#f92672">import</span> ArraySum
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ArraySumTests</span>(unittest<span style="color:#f92672">.</span>TestCase):
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">setUp</span>(self):
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">pass</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_for_int</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>,])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(type(result) <span style="color:#f92672">is</span> int, <span style="color:#e6db74">&#34;Result is not integer&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_flat_list</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">15</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_complex_list</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#e6db74">&#34;hello&#34;</span>])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">6</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">test_complex_nested_list</span>(self):
</span></span><span style="display:flex;"><span>        result <span style="color:#f92672">=</span> ArraySum([<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#e6db74">&#34;hello&#34;</span>, [<span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>]])
</span></span><span style="display:flex;"><span>        self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">15</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
</span></span><span style="display:flex;"><span>    unittest<span style="color:#f92672">.</span>main()</span></span></code></pre></div>

Before we make any changes to `ArraySum`, we have to run our test.
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">.</span>F<span style="color:#f92672">..</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">======================================================================</span>
</span></span><span style="display:flex;"><span>FAIL: test_complex_nested_list (__main__<span style="color:#f92672">.</span>ArraySumTests)
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Traceback (most recent call last):
</span></span><span style="display:flex;"><span>  File <span style="color:#e6db74">&#34;tests.py&#34;</span>, line <span style="color:#ae81ff">22</span>, <span style="color:#f92672">in</span> test_complex_nested_list
</span></span><span style="display:flex;"><span>      self<span style="color:#f92672">.</span>assertTrue(result <span style="color:#f92672">==</span> <span style="color:#ae81ff">15</span>, <span style="color:#e6db74">&#34;Incorrect sum&#34;</span>)
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">AssertionError</span>: Incorrect sum
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">4</span> tests <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>FAILED (failures<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>)</span></span></code></pre></div>

Now that we have a failed test, we are given the okay by the testing goat to modify our code. Let's add our recursive call, so our function now looks like:
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ArraySum</span>(int_list):
</span></span><span style="display:flex;"><span>    sum <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> x <span style="color:#f92672">in</span> int_list:
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> type(x) <span style="color:#f92672">is</span> int:
</span></span><span style="display:flex;"><span>            sum <span style="color:#f92672">+=</span> x
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">elif</span> type(x) <span style="color:#f92672">is</span> list:
</span></span><span style="display:flex;"><span>            sum <span style="color:#f92672">+=</span> ArraySum(x)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> sum</span></span></code></pre></div>

Re-running our final test...
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">....</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">----------------------------------------------------------------------</span>
</span></span><span style="display:flex;"><span>Ran <span style="color:#ae81ff">4</span> tests <span style="color:#f92672">in</span> <span style="color:#ae81ff">0.000</span>s
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>OK</span></span></code></pre></div>

Ahhhhh :)