Skip to main content

Testing In Python...and my Two cents

The only lesson I have learned in mys past one and half of years of experience in testing is that the easier I make writing and running tests for my programs, the faster I can produce bug-free software.

Being a developer and also being a tester can do wonders I think. Never before this thought would have crossed my mind while coding.


That is why I start most projects nowadays with two pre-baked files: main.py and tests.py.
With this setup and tools like Nose, testing code in Python for me is nearly pain free. Recently however, I notice one aspect of the unittest package in Python's standard libary drives me absolutely crazy:
Why is there no simple method to run a single test case from within an interactive python shell?
As developers, this is important because if we are going to make writing tests an integral part of our development workflow, one needs some way to actually run the damn test without dropping out of our Python sessions and breaking out of our mental flow.
Being forced to run tests solely from the commandline also means we can't take advantage of features like ipython's ability to automatically drop into a pdb debugging session when an error occurs. This is extremely useful for when you want to introspect one or two variables to determine why a test failure occurred.
In general, it seems like the unittest library's lack of a simple function to run single test cases in an interactive shell dis-incentivizes programmers from writing tests and detracts from python's whole "rapid iteration" ethos.
After googling around and failing to find any good alternatives, I've finally settled on writing my own utility function to run single test cases:

def runtest(testCase, methodName):
    """ Runs a test case from within interactive shell """

    tc = testCase(methodName)
    getattr(tc, "setUp", lambda:None)()
    try:
        getattr(tc, methodName)()  
    finally:
        getattr(tc, "tearDown", lambda:None)()

I add this function to every single project that I create now, and the qualitative feel of writing tests just feels so much more natural now. I also find myself writing more tests and running them more often with this function in my projects.
Give it a try, and let me know whether this has any effect for you. I'd love to hear your feedback on how it affects your development workflow.

Popular posts from this blog

LibrePlanet 2017: Liberating your open source experience

LibrePlanet is a yearly gathering of free software activists, users, and contributors—and, it's my favorite conference of the year. Here's why.
LibrePlanet is run by the Free Software Foundation, and has steadily evolved from a yearly members' meeting with presentations from staff and board members to a full blown two-day conference with speakers and attendees from all over the world. The event brings people who care about free software together to talk about the future of the movement, address current challenges, and celebrate successes.
Prelude I was invited to give a talk at LibrePlanet 2017 on 25th March at MIT, in Cambridge, Massachusetts representing Mozilla as a Tech Speaker. I reached Boston on 25th early morning. Around 1 AM. The journey itself was awesome till I realized that you don't get Uber or Lyft at Boston Airport.

Not that the apps don't function there. They work! Just no driver will be ready to pick you up from Airport at that time. After trying t…

Bringing the Focus back : Firefox Focus (Builds) for Android

Firefox Focus – A Free, Fast Private Browser for....android! On 17th November 2016 Mozilla announced Firefox Focus. A free fast and easy to use private browser for iOS. Firefox Focus was filled with goodies. From inbuilt tracker blocking, content blockers to making privacy the first class citizen. It was all of that. Wrapped in a nice package, but only for Apple Ecosystem. The argument for having focus was to make privacy dead simple and default experience for most people out there. An excellent read is this article "Privacy made simple with Firefox Focus".
And while this was all fine, a lot of us were severely disappointed that we don't have an android version. That all changes now.
Mozilla has released a port of the Firefox Focus source code and I decided to build a port from it. And this is how it looks in my One Plus One.
If you notice it looks almost similar to its iOS counterpart. Focus blocks tracking cookies by default in its system. But there are small design c…

TechSpeakers Ahoy! : A Berlin Meetup

Mozilla Foundations is a curious organization. Though most of us have grown up getting acquainted with it by using Netscape and later Firefox, Thunderbird. There is also the foundation part where it undertakes a lot of projects to make the openweb and world as a whole a better place.
Among many teams that help volunteers, budding developers dip their toes into code, help in evangelism, code contribution, advocacy is Developer Relations (DevRel) and they came up with an ingenious plan to come up with a team of volunteers passionate about these to come forward represent Mozilla DevRel. Havi Hoffman (from Mozilla Labs), Dietrich Ayala (From Platfrom Engineering, Firefox OS) and many others handpicked a group of people who eventually came to know as Mozilla TechSpeakers. (If you want to read more about it, this lovely piece by Havi is a must read!). I was fortunate enough to be in the first group (knows as pilot pilots). And eventually ended up being facilitator to the last group which gr…