Skip to main content

Return of Python Unittests

I got into the habit of using Python unit tests in a job a few about a year ago. I eventually created the jsonhunt.py unit test in my Toys repository but it stopped working. I haven't even used the jsonhunt script in a while and am no longer certain of its usefulness but I don't like the idea of not being able to do unit tests if I wanted to.

Simple working example

So I sat down and got a simple unit test working. Here's the test script:
#! /usr/bin/env python
import os
import logging
from unittest import TestCase
import random
logging.basicConfig(format='%(asctime)s %(levelname)s %(pathname)s:%(lineno)d %(msg)s')
log = logging.getLogger()
class Tests(TestCase):
def __init__(self, *args, **vargs):
super(Tests, self).__init__(*args, **vargs)
log.setLevel(int(os.environ.get('LOG_LEVEL') or '30'))
def test1(self):
log.info('This is a test')
self.assertTrue((random.randint(0,9) % 2) == 0)
view raw test1.py hosted with ❤ by GitHub

Notes

  • I wish I had found a way to see options or arguments in the test but I couldn't find a way to do that. I had to settle for an environment variable to control the logging level.
  • From the job I mentioned, I remembered we had a way of specifying the log level through some sort of option but I couldn't find a opportunity to get such an option, even through the superclass.
  • At first I was trying to use a zero-argument constructor method but that wasn't working so I added *args, **vargs.

From the command line

I have a copy of the script in a git repository so a way to run the test is using that copy of the script:
  1. get clone https://github.com/pfuntner/fun.git
  2. cd fun/test
  3. python -m unittest test1.Tests.test1

From PyCharm

Using the same repository as above:
  1. Install the pytest package in PyCharm if it's not already installed
  2. Create a PyCharm Run Configuration using the pytest Python test template:

Notes

  • I couldn't get the PyCharm debugger working - at least not yet!

Comments

Popular posts from this blog

Dynamic Python script loading

I have a bunch of toys and tools in a Git repository - I affectionately call this my toys repo . Most are just scripts that I use from a Unix (Cygwin or Git bash on Windoze) command line but there are some Python classes that I sometimes use in another script. Today at work, I was coming up with a new Python script that made use of a couple of my public classes. The script is good enough to share with my colleagues but I'm faced with the problem of my public classes: I imagine that most of my colleagues haven't even heard of my public classes and I don't expect them to download the entire repo just to get a couple of classes If I'm going to distribute the classes as separate files I introduce new problems: It could be confusing to have the files tag along. What is the user supposed to do with them? The answer is nothing - they should leave them alone and make sure they are in the same directory as the main script in case they decide to move th...

Git-based version information from Python script

I had this idea of generating version information for a Python script that uses ArgParse . The code is a little more than I was expecting but I think it works well. Here is the code: Usage Here is an example of its usage if the script is part of a git repository: $ ./version-example --version b92798, master, 2019-01-18 10:35:02, ['origin:https://github.com/pfuntner/gists.git'] $ It contains: The SHA1 of last git commit that changed the script The current branch of the repository The date of the commit - I think the timezone element is present in this but I didn't want to deal with timezones so I'm ignoring it A list of the remote repositories This is printed on two lines but that's something that ArgParse is doing, not me. Here is an example of its usage if the script is not part of a git repository - we don't have much information to work from but we can at least get the timestamp of the script: $ ~/tmp/version-example --version 2019-01-...

Extra Cygwin packages

Extra Cygwin packages I'm a big fan of Cygwin on Windoze to get the Unix/Linux look and feel. I can't stand the Windoze command prompt and their Power Shell is ridiculous. I don't want to get on another rant but I thought Power Shell was supposed to be their attempt at making Windoze more like Unix but it isn't. Yes, I have looked into WSL and it's ok but I'll stick with Cygwin. One of the things I don't like about Cygwin is that their default list of packages just isn't enough for me. After I install it on a new system, I often find myself adding additional packages one-at-a-time when I discover I need them. I finally decided to be a little proactive and keep track of packages I had to install. It looks like my beautiful formatting of this page has been trashed somehow and I apologize for that. I'll work on it. My extra packages Package Comments git Gotta have this! pip2 Yes, I'm still a proud Python2 guy! An...