Unit tests for functions in a Jupyter notebook?
<p>I have a Jupyter notebook that I plan to run repeatedly. It has functions in it, the structure of the code is this:</p>

<pre><code>def construct_url(data):
    ...
    return url

def scrape_url(url):
    ... # fetch url, extract data
    return parsed_data

for i in mylist: 
    url = construct_url(i)
    data = scrape_url(url)
    ... # use the data to do analysis
</code></pre>

<p>I'd like to write tests for <code>construct_url</code> and <code>scrape_url</code>. What's the most sensible way to do this?</p>

<p>Some approaches I've considered:</p>

<ul>
<li>Move the functions out into a utility file, and write tests for that utility file in some standard Python testing library. Possibly the best option, though it means that not all of the code is visible in the notebook.</li>
<li>Write asserts within the notebook itself, using test data (adds noise to the notebook).</li>
<li>Use specialised Jupyter testing to test the content of the cells (don't think this works, because the content of the cells is going to change). </li>
</ul>