5

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?

6
  • How do you start nose exactly? What is name of the file you want to test and what is complete content? (or content as short as possible, but still showing the problem). What do you expect nose to do? Commented Jun 24, 2014 at 22:19
  • It looks like the nosetests executable thinks verify is an argument rather than a file. Perhaps you could give it the full path to your verify.py file or run nosetests from a more suitable location? Commented Jun 24, 2014 at 23:11
  • @JanVlcinsky and Yani Please check my answer it's work for me. If you have any additional ideas please add some answers or put some comments thank you for your help. Commented Jun 25, 2014 at 9:04
  • @teces907 Actually, the problem can be much simpler -your call to nosetests is passing argument verify and it shall be verify.py Commented Jun 25, 2014 at 9:31
  • 1
    @terces907 No need to apologize for poor experience. You are improving it and in fact, it is never ending iterative process. Commented Jun 25, 2014 at 11:10

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.