Monday, June 20, 2011

Skipping tests in Python unittests

There are situations when writing unittests in Python you want to skip some tests - for example, you don't want to initiate process that could take some time to finish, e.g parsing huge XML.

In that case, Python's unittest allows you to annotate which of your methods in your test case you'd like to skip. Starting from Python 2.7, you could use any of these skip decorators in your code:

1. @unittest.skip
2. @unittest.skipIf
3. @unittest.skipUnless

Here's an example on how to use them -
import sys
import unittest

class TestSample(unittest.TestCase):
    @unittest.skip('Unconditional skipping of the test method')
    def test_skip_unconditional(self):
        self.assertTrue(True)

    @unittest.skipUnless('-smoke' in sys.argv,\
                         'Skipping the smoke test')
    def test_skip_unless_conditional(self):
        self.assertTrue(True)

    @unittest.skipIf('-quick' in sys.argv,\
                     'Skipping a time consuming test')
    def test_skip_if_conditional(self):
        self.assertTrue(True)

if __name__ == '__main__':
    # don't pass the args to unittest module
    try:
        sys.argv.remove('-quick')
    except ValueError:
        pass

    try:
        sys.argv.remove('-smoke')
    except ValueError:
        pass

    unittest.main()

The test test_skip_unconditional is skipped all the times. The test test_skip_unless_conditional is run only if the unittest is invoked with -smoke option while the test test_skip_if_conditional is skipped if the unittest is invoked
with -quick option.

Here's the result of running the code above:

$ python sample_tests.py -v
test_skip_if_conditional (__main__.TestSample) ... ok
test_skip_unconditional (__main__.TestSample) ...
skipped 'Unconditional skipping of the test method'
test_skip_unless_conditional (__main__.TestSample) ...
skipped 'Skipping the smoke test'

-------------------------------------------------------------
Ran 3 tests in 0.000s

OK (skipped=2)

As you can see from the result, it shows how many of our tests were skipped and if the test was successful or not. Apart from these commonly used decorators, the unittest module has more and equally useful ones you should check out.

Since discovering Python in the last couple of years, I continue to be blown away at how flexible and refreshing it is to program in a language as cool as this. Unlike Java and other languages, testing modules come as default with your standard Python installation. Apart from this default testing module, you might want to see another one. Pytest.org provides another awesome testing framework that is quite easy to use. The site has a huge documentation to get you started - so check that sites out to learn how to do more with testing your Python code.

But if like me you prefer not to download additional testing framework, then check out Unittest tutorial on Python site for more on how to write Python test code.

Monday, June 13, 2011

My Experience Learning A New Programming Language

Since I left university several year ago, I have been programming heavily in Java and Python. In fact, my main language right now is Python. I have talked really well about it in this blog; and how I got into it in the first place.

If you came through any sort of university route, you will have been taught Java as well during your time there. But as you get into the industry proper, it soon becomes clear that you need more than Java to compete. You need more than just one programming language under your belt to get you anywhere as a software professional. What you learnt as part of your course is nothing compare to what you will be faced with in your day job. So, that's why it is important to have a good idea of what it takes to succeed as a software professional.

One of the best qualities of a professional software developers is the ability to adapt, learn and use the right tool or language for the task in hand.

We recently started working on a Windows-based project and we were all give a copy of Beginning C# Object-Oriented Programming to get us up to speed with C#. Although I have been programming for as long as I care to remember, I had never do any Microsoft Windows based development at all. Most of my work have been on Linux or Mac-OSX which will make this project a nice opportunity to explore Windows proper.

I spend the weekend on this Beginning C# Object-Oriented Programming and was surprised to see how easy it is to work with the .NET framework. If you already know Java and other Object Oriented programming, you can easily skip some of the chapters in this book. The first few chapters cover the logic and intro into .NET programming while the rest move deeper into designs along with more than 30 fully hands-on activities, you'll discover how to transform a simple model of an application into a fully-functional C# project, including designing the user interface, implementing the business logic, and integrating with a relational database for data storage. Along the way, you will explore the .NET Framework, the creation of a Windows-based user interface, a web-based user interface, and service-oriented programming, all using Microsoft's industry-leading Visual Studio 2010, C#, Silverlight, the Entity Framework, and more.

I totally recommend this book if you are getting into Object oriented development for C# and .NET. I didn't think I would like it, but was blown away at the pace, content and style of the writing. You won't be surprised to learn that the author, Daniel Clark, is a trainer.

Wednesday, June 08, 2011

Pro Python by Marty Alchin - Book Review

Pro Python - This is one of those books you need to really spend time on to seriously appreciate what a great technical book should be.

I received this book a while ago, other than the quick flip through the chapters, I didn't spend time reading it until now. I bought it during a project we were working on and needed something that would give me real practical ways on how to use Python. Most of the books I started with only touched on the basics as listed in the free Python book.

But Pro Python is entirely different. It contains one of the full description of decorator patterns in Python I've seen in any books. Despite spending countless hours on the web, no one actually explained those patterns. It might be that they don't know it or can't be bothered to go that deep. That's exactly where this book shines - it addresses some of the topics you will find useful for real life projects like the credit card processing application we were working on.

Despite being a pro book, it breaks down advanced topics into basics, functions, classes, meta programming, and includes distribution, not always covered by other texts.

This book does more to explain the Python mindset needed for clear, concise code, than some of the explanations I have read that seem to favor dictating a “Python Way” to the reader. Therefore, once you have learned Python basics, this book is a must for your library.

Bon Apetit!

Sunday, June 05, 2011

Western Digital My Book Essential 3 TB USB 3.0/2.0 Desktop External Hard Drive

My Book Essential 3 TB
I've been working with computers since 1994, back when there were no personal or micro-computers. Back when a 300 baud modem was hot stuff. My first 60MB hard drive cost $600 (back when $600 actually meant something) and measured about a foot long or more, about 8 inches high and six inches wide. So you can imagine my delight when this little 2TB drive showed up. It's no more than a tenth of the size of that first 60MB hard drive. Imagine, two thousand gigabytes.

There's an old saying in computing (or at least those old enough to remember it) that, no matter the size of your hard drive, you'd only have about 1MB of free space. Well, not no more. Given the performance of our other Western Digital drives, I'm pretty confident that the My Book drive will last until, well, until we don't need to preserve our professional, historical files anymore.

Now, before I buy things like terabyte drives or printers or laptops, I always check PC Magazine's Editor's Choice collection, and this one was highly recommended. Its speed, size, and simplicity make it a real standout. Furthermore, we've been using Western Digital's drives for over ten years and have been very pleased with them.

Unfortunately, though, Robin's Rule #2 is: all drives fail eventually, as do computers. (Robin's Rule #1 is: never pass a law you can't enforce, but we'll skip that one here.) My sturdy old Pentium 4 Micron computer is finally dying, after putting in several years of hard service, so it's time to move on to the laptops, especially now that we're retired and I'm no longer doing interface or other design work that really does need a large monitor. And moving to the laptops means it's time to transfer all our historical, professional data off the old machines, which is why I bought the My Book drive.


The real beauty of the Western Digital My Book isn't just its thundering capacity, it's the size and portability. Using a simple USB cable, I can move my main development data from one computer to the next in less than five minutes (Gee, just like the old days of moving SCSI drives on the Macs). So now every one of our computers with a USB port can become my main development and writing computer. Just as soon as I transfer that 1.5GB of old emails from clients and contact information for everyone I know (I used to use Outlook Express, but now I just let Yahoo take care of the mail), and things like my book-selling database files. The total data transfers have now taken up barely a fraction of the My Book's space. Out of a readable 1.81TB, I still have 1.78TB left. And that represents over fifteen years of developing database applications for clients like California state agencies and medical research projects.

Meanwhile, the My Book drive just sits quietly in the background, taking up almost no space and storing our entire professional history of database development. Yes, it's a ridiculously huge drive for a small company, but all those historical files are critical and, what with the recent tornado warnings (tornadoes in California? Really? Weird.), it's nice to know that all I need to do is unplug this one little drive and our critical data is safe. Or, at least, it will be safe just as soon as I finish transferring the data from the other computers sitting around here. That's an awful lot of reassurance for about $130.
Related Posts with Thumbnails