{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# Code Testing\n", "\n", "- smoke tests\n", "- unit tests\n", "- `pytest`" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "slide" } }, "source": [ "## Writing Good Code" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "source": [ "All in all, write code that is:\n", "\n", "- Well organized (follows a style guide)\n", "- Documented\n", "- **Tested**\n", "\n", "And you will have understandable, maintainable, and trustable code. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #1\n", "\n", "Given the following code, which assert will fail?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def extend(input_arg):\n", " output = input_arg.copy()\n", " for element in input_arg:\n", " output.append(element)\n", " return output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# test here" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- A) `assert type(extend([1, 2])) == list`\n", "- B) `assert extend([1, 2]) == [1, 2, 1, 2]`\n", "- C) `assert extend((1, 2)) == (1, 2, 1, 2)` \n", "- D) `assert extend(['a', 'b', 'c']) == ['a', 'b', 'c', 'a', 'b', 'c']`\n", "- E) `assert extend([]) == []`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Clicker Question - Asserts" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check that extend returns a list\n", "assert type(extend([1, 2])) == list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check that an input list returns the expected result\n", "assert extend([1, 2]) == [1, 2, 1, 2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check if the function works on tuples\n", "assert extend((1, 2)) == (1, 2, 1, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check that a different input list (different lengths / contents) returns expected result\n", "assert extend(['a', 'b', 'c']) == ['a', 'b', 'c', 'a', 'b', 'c']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check that an empty list executes, executing an empty list\n", "assert extend([]) == []" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "slide" } }, "source": [ "## Code Testing" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
print statements as its output, it will *not* be testable. Consider this during development/planning!\n",
"