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?
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 .
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.
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.
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
comp.py
if __name__ == "__main__"
block. This allows safe importing of the module and also running the code, when called as scriptmain
functionafunc
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 testableHere 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()
veryfi.py
=> test_it.py
test_(something).py
. Nose will then discover the file automatically.afunc
test_with_gen
.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With