Sitemap

Functional Creativity

5 min readOct 23, 2019

--

Press enter or click to view image in full size

For the longest time — for me — creativity has been framed with words like spontaneous, uninhibited and natural. For the most part, creativity was a method of process. A method of imagination towards an infinite end. No real consequence if the final product was never really finished and could be philosophically wrapped in an aesthetic of ‘nothingness’. Creativity — for me — was an engagement similar to the endless fractals of Jackson Pollock’s action paintings; or the meditative routine of Agnes Martin’s mark making. It was for its own sake and fundamentally an exercise in the act of creating. But who was that actually for?

Ever since I’ve been working with Python to analyze data I’ve found myself in an interesting mindset. First, creativity became more than an exercise. It became a method of problem solving; one that can serve as a solution to others in addition to practice for the self. Secondly, Python engaged creativity as a function with an end: to systematically develop a process which provides insights. The method to solutions and recognition of trends is a fascinating art when it’s designed accurately. But how can we assure an accurate design?

This blog is a brief introduction to a method of testing Python code by creating a test module with unittest.

WHAT IS UNITTEST?

Simply put: unittest is a module in the standard library of Python. Using it parallel with a script you’re working on can help you create reliable, flexible and intentional code.

Here is a link to Python unittest documentation.

Below is a set of elementary functions, followed by a unittest module designed to test the accuracy of the functions.

Functions

Note: I saved the below five functions as filename: BLOG_CODE_1.py. This is important as you’ll see I’ve imported BLOG_CODE_1 along with unittest in the following module.

Test Module with unittest

Note: I saved this module as filename: test_funct.py. This is important due to how you will run the test in your terminal. See comments in bottom of code as well as Step 1.

Okay — so let’s break this down.

Step 1: Naming your Module.

How you name the test module is crucial. Here it is named: test_funct.py. Using this naming convention allows your terminal to read the file as a test. You will find that if you name the file something else, say: funct_test.py, you’re terminal will not run the test accurately if at all, rendering no useful results. Naming convention: ‘test_filename.py’

Step 2: Import

Unittest is native in Python. Simply import it. Additionally, import the script you’d like to test as well. If you don’t know the name you’d like to use for your script file, then add it later.

Get Matthew R Finch’s stories in your inbox

Join Medium for free to get updates from this writer.

Step 3: Basic Order and Procedure

  1. Designing the Test

Due to the elementary nature of BLOG_CODE_1, it may not be obvious that I designed the module first. This is where the functional creativity comes in. Using comments, I outlined how I imagined the purpose for each function in the test. From there I designed how the tests would interact with the code I would write to test the accuracy of the intention behind the code. That being said, while you build your class, it’s okay to leave a placeholder in the class name where you’ll see the script’s name proceeding def (see below):

def Test[scriptname](unittest.testcase)

This may be seem tedious, and if anyone else has some other suggestions, please comment at the end of the blog — but there’s a reasoning behind this. To me, a test is the blueprint to code. As I mentioned, with comments in your text editor, you’ll make a rough sketch how each test will shape the code’s functionality. Then begin to fill in the logic and design the intention behind each line. In unittest’s documentation, you’ll find a variety of assertions that will be the syntax of your tests. I used mostly ‘assertEqual’ due to the nature of the code I knew I would write for this example.

Imagination is key here. You will not want to limit your assertions to standard use of your code. Get that mind spinning up some possibilities of how an input could potentially break your code and be sure to account for edge cases in your assertions. If you’re building scripts for open source domains, you won’t want messages bombarding you that your script broke because the user thought it would be able handle something you didn’t think of! Additionally, this is even more important if you’re designing customizations for code specific to a product for a client. Either way — to me, this is fun the part.

2. Writing the Script

The script should come easy at this point, like coloring in a template. However, there’s still creativity here. As I was writing the subsequent script, I found my self continuing to question how I could test the functions I was defining. Nothing too fancy surfaced, as the example for this blog didn’t call for it; but I had the test screen parallel in a right split in VSCODE the whole time, and it was a last minute addition to place result_edge_2. Continuing the metaphor: as you color the template, you may realize there are more sides or angles to your architecture. By having the module open along side your script, making quick redesigns or additions to your test is simple — and at times, inspiring.

3. Running the Test

What a moment of excitement — or potential anxiety. To run the test, just open your terminal and from the directory your files are located in, run:

python3 -m unittest filename.py

If the test was a success you’ll see:

Press enter or click to view image in full size

Note the number of dots equal the number of successful tests run and correspond to each function in the test case.

If the test spotted an error, you’ll receive Python’s lovely feedback. I broke the code on the last test in this example. Here is the error message. Notice that the first 4 tests ran smoothly, noted by the dots, until ‘E’ on the last test:

Press enter or click to view image in full size

There are other testing methods and I encourage you to dive into them. PyTest is another one I like.

While it may seem like just an added step to what can already be a lengthy process, testing your code will not only assure its accuracy, allow you to make it flexible and custom when needed as well as give deeper understanding to how your script is operating — more importantly, it will save so much time when debugging, and more interestingly, allow you to make further creative decisions in modulating, updating and evolving your code to newly imagined functionality as new solutions are needed — without running into errors.

One last thing. You’ll notice I’m running:

python3 test_funct.py

By adding:

if__name__=='__main__':
unittest.main()

at the bottom of you’re test module, you’ll be able to run your test with fewer keystrokes in the terminal.

--

--

Matthew R Finch
Matthew R Finch

Written by Matthew R Finch

Data Scientist | Machine Learning Engineer | Creative and Critical Thinker