Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not run nosetests when i use argparse in my python code

In my main code I have this:

  #comp.py
  parser = ArgumentParser()
  parser.add_argument("-n", dest="deg", default=100,type=int, help="setup value of deg")
  parser.add_argument("-k", dest="k", default=25, type=float, help="setup value of k")
  parser.add_argument("-l", dest="l", default=0, type=int, help="setup value of l")
  args = parser.parse_args()

  def afunc(x):
      ...
      #do something with k, l, deg and the return the result
      ...
      return result

and my testing file verify.py:

  #verify.py
  import unittest
  import comp
  class TestFuncs(unittest.TestCase):
      def test_afunc(self):
          self.assertEqual(afunc(0), 0)
          self.assertEqual(afunc(1), 0)
          self.assertEqual(afunc(1), 1)
          self.assertEqual(afunc(3.2), 1)
...

And when I tried to run nosetests for testing results of function afunc(...), I got this error:

machine:project user$ nosetests verify
usage: nosetests [-h] [-n DEG] [-k K] [-l L]
nosetests: error: unrecognized arguments: verify

How to solve this problem?

like image 526
fronthem Avatar asked Jun 24 '14 22:06

fronthem


People also ask

How do I run a Unittest in Python?

The command to run the tests is python -m unittest filename.py . In our case, the command to run the tests is python -m unittest test_utils.py .

Which function in unit test will run all of your tests?

TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.

Does Python come with Argparse?

The Python argparse library was released as part of the standard library with Python 3.2 on February the 20th, 2011. It was introduced with Python Enhancement Proposal 389 and is now the standard way to create a CLI in Python, both in 2.7 and 3.2+ versions.


2 Answers

Ok, I just solved the problem by adding a few lines of if else condition.

It seems to be my test file (verify.py) can not manage value assignment on the parser section in comp.py. So, I just add some condition below to assign the values of deg, k, l in case that comp.py doesn't run as a main function.

#comp.py
if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("-n", dest="deg", default=100,type=int, help="setup value of deg")
    parser.add_argument("-k", dest="k", default=25, type=float, help="setup value of k")
    parser.add_argument("-l", dest="l", default=0, type=int, help="setup value of l")
    args = parser.parse_args()
else:
    deg=100
    k=25
    l=0

def afunc(x):
    ...
    #do something with k, l, deg and the return the result
    ...
    return result
like image 156
fronthem Avatar answered Oct 11 '22 01:10

fronthem


Updated comp.py

  • run the actual code only from if __name__ == "__main__" block. This allows safe importing of the module and also running the code, when called as script
  • separate argument parsing into main function
  • keep the tested afunc as independent from the rest as possible, so expect all needed input to be passed via arguments, not via sys.argv or other global state. This makes your function much more testable

Here comes the code:

from argparse import ArgumentParser

def main():
    parser = ArgumentParser()
    parser.add_argument("-n", dest="deg", default=100,type=int, help="setup value of deg")
    parser.add_argument("-k", dest="k", default=25, type=float, help="setup value of k")
    parser.add_argument("-l", dest="l", default=0, type=int, help="setup value of l")
    args = parser.parse_args()

def afunc(x):
    return 2 * x

if __name__ == "__main__":
    main()

Updated veryfi.py => test_it.py

  • call the file test_(something).py. Nose will then discover the file automatically.
  • define clear focus of your test, in this case ignore the argument parsing and test only the afunc
  • (recommended) split to smaller test cases - your test run report will tell you much better, what really went wrong.
  • (nose feature) use test generator as shown with test_with_gen.
  • possibly move the test(s) into tests subdirectory. Just a habit. And it allows to target nose to tests with better precision.

Here is updated test_it.py:

import unittest
from comp import afunc

class TestFuncs(unittest.TestCase):
  def test_afunc(self):
      self.assertEqual(afunc(0), 0)
      self.assertEqual(afunc(1), 2)
      self.assertEqual(afunc(2), 4)
      self.assertEqual(afunc(3.2), 6.4)

def test_case_0():
    assert afunc(0) == 0

def test_case_1():
    assert afunc(1) == 2

def test_with_gen():
    plan = [(0, 0), (1, 2), (2, 4), (3.2, 6.4)]
    for arg, expected in plan:
        yield check_afunc, arg, expected

def check_afunc(arg, expected):
    assert afunc(arg) == expected

Run the test:

$ nosetests -v
test_afunc (test_it.TestFuncs) ... ok
test_it.test_case_0 ... ok
test_it.test_case_1 ... ok
test_it.test_with_gen(0, 0) ... ok
test_it.test_with_gen(1, 2) ... ok
test_it.test_with_gen(2, 4) ... ok
test_it.test_with_gen(3.2, 6.4) ... ok

----------------------------------------------------------------------
Ran 7 tests in 0.005s

OK
like image 26
Jan Vlcinsky Avatar answered Oct 11 '22 02:10

Jan Vlcinsky