unittest and argparse

3,148 views
Skip to first unread message

Blanca Mancilla

unread,
Sep 8, 2015, 8:57:11 AM9/8/15
to montrea...@googlegroups.com
Hi,

Does anybody have experience using unittest to test programs using argparse? I am using Python 3.4.3

I am sure this is a newbie problem!

Thanks!

bm

--
Nothing is more expensive than a missed opportunity.

Hao Deng

unread,
Sep 8, 2015, 8:59:43 AM9/8/15
to montrea...@googlegroups.com
>>> import sys
>>> sys.argv
['/usr/bin/bpython']
>>> sys.argv.append('hello')
>>> sys.argv
['/usr/bin/bpython', 'hello']

I guess you can modify sys.argv to simulate that.


--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "Montréal-Python".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse montrealpytho...@googlegroups.com.
Pour envoyer un message à ce groupe, envoyez un e-mail à l'adresse montrea...@googlegroups.com.
Visitez ce groupe à l'adresse http://groups.google.com/group/montrealpython.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

vahan

unread,
Sep 8, 2015, 9:41:54 AM9/8/15
to montrea...@googlegroups.com

-- 
vahan

--

Marc Tardif

unread,
Sep 8, 2015, 10:27:18 AM9/8/15
to montrea...@googlegroups.com
Blanca, code that is hard to test is also hard to understand. This is
usually a good opportunity to refactor your code. For example, argparse
could be isolated to a function like:

def parse_args(args):
parser = ArgumentParser()
parser.add_argument("--foo", action="store_true")
return parser.parse_args(args)

This function can be tested clearly like this (I'm using py.test):

def test_parse_args_default():
opts = parse_args([])
assert not opts.foo

def test_parse_args_foo():
opts = parse_args(["--foo"])
assert opts.foo

Vahan, you should only mock what you own; you shouldn't mock a third party
library. Here's a good article on the subject with other suggestions:

https://practicingruby.com/articles/mock-objects-deeply-influence-design

* vahan <aiv...@gmail.com> [2015-09-08 09:41 -0400]:
> use mock https://pypi.python.org/pypi/mock
>
> On Sunday, September 6, 2015 at 7:12 PM, Blanca Mancilla wrote:
> > Does anybody have experience using unittest to test programs using
> > argparse? I am using Python 3.4.3
> >
> > I am sure this is a newbie problem!

--
Marc Tardif <ma...@interunion.ca>
Freenode: cr3, Jabber: c...@jabber.org
1024D/72679CAD 09A9 D871 F7C4 A18F AC08 674D 2B73 740C 7267 9CAD

Benoit

unread,
Sep 8, 2015, 11:13:10 AM9/8/15
to montrea...@googlegroups.com
Hey Blanca, I agree with Vahan that "mocking" is the best approach. Here's a quick and dirty example inspired by http://stackoverflow.com/questions/18668947/how-do-i-set-sys-argv-so-i-can-unit-test-it


import unittest
from unittest.mock import patch
import sys

def thing_that_uses_argparse():
  return sys.argv

class TestThings(unittest.TestCase):
    def test_parse_args(self):
        testargs = ["one", "two"]
        with patch.object(sys, 'argv', testargs):
            things = thing_that_uses_argparse()
            print("Inside with: %s" % things)
            assert(things == ["one", "two"])
        print("Outside with: %s" % thing_that_uses_argparse())

if __name__ == '__main__':
    unittest.main()

 

Benoit Clennett-Sirois

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes Montréal-Python.
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse montrealpytho...@googlegroups.com.
Pour envoyer un message à ce groupe, adressez un e-mail à montrea...@googlegroups.com.

Visitez ce groupe à l'adresse http://groups.google.com/group/montrealpython .
Pour plus d'options, visitez le site https://groups.google.com/d/optout .

vahan

unread,
Sep 8, 2015, 11:18:08 AM9/8/15
to montrea...@googlegroups.com
So this author basically says: “I never used a hammer because I dont know how to use it. I use my forehead to drive the nails instead, so should you”.


-- 
vahan

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes Montréal-Python.
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse montrealpytho...@googlegroups.com.

Benoit

unread,
Sep 8, 2015, 11:40:56 AM9/8/15
to montrea...@googlegroups.com
Obviously it's better to use proper indentation, unlike my example :)

Benoit Clennett-Sirois

Blanca Mancilla

unread,
Sep 8, 2015, 6:29:33 PM9/8/15
to montrea...@googlegroups.com
Thanks for all the comments. I am figuring out what I need and want to do. I still can't get it to run. 

As a general question, is unittest a good testing framework?

bm

Pour envoyer un message à ce groupe, envoyez un e-mail à l'adresse montrea...@googlegroups.com.
Visitez ce groupe à l'adresse http://groups.google.com/group/montrealpython.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Marc Tardif

unread,
Sep 9, 2015, 11:19:19 AM9/9/15
to montrea...@googlegroups.com
Yes, unittest is a good testing framework and it should be sufficient to
suit the needs of many projects. If code seems hard to test with unittest,
I would first consider simplifying the code before switching to another
framework. An advantage of this framework is that it's part of the Python
standard lib, so your project doesn't need more dependencies and more
potential contributors to your project are likely to be familiar with it.

That being said, there are indeed other testing frameworks. Personally, I
really like pytest because it is one of those rare projects that have
pleasantly surprised me on several occasions by just doing the right
thing. I also like how pytest feels pythonish whereas unittest feels
javaish because it was intended to be easy to use for people familiar
with other the xUnit frameworks. It also has strong support for fixture
and state management which can probably be achieved with unittest but
feels more natural with pytest.

For other testing frameworks, I really like this taxonomy:

https://wiki.python.org/moin/PythonTestingToolsTaxonomy

* Blanca Mancilla <blancal...@gmail.com> [2015-09-08 18:29 -0400]:
> Thanks for all the comments. I am figuring out what I need and want to do.
> I still can't get it to run.
>
> As a general question, is unittest a good testing framework?
[snip]
Reply all
Reply to author
Forward
0 new messages