I am writing some unit tests
for my python script. The script allows the user to override a parameter's value.
python paramParser.py -d C:\\Path\\to\\yourfile\\xxx.m -p 'xxxParamName' -v 20
-d
filename which could contain a parameter name and value
-p
parameter name
-v
parameter value
The script reads the file and if the parameter exist in it - it will take its value and write it to another file with its new value.
Running this from the command-line
successfully overrides the parameter's value.
Here is my code:
import unittest
import paramParser
class Tests(unittest.TestCase):
def testOneIntValue(self):
result = paramParser.main(["-d C:\\Path\\to\\yourfile\\xxx.m", "-p 'xxxParamName'", "-v 3"])
self.assertTrue('xxxParamName.Value= 3', result)
def testMultipleIntValues(self):
result = paramParser.main(["-d C:\\Path\\to\\yourfile\\xxx.m", "-p 'xxxParamName'", "-v 3"])
self.assertTrue('[ xxxParamName.Value = [ 1 2 3 ]', result)
def testTrueBoolean(self):
result = paramParser.main(["-d C:\\Path\\to\\yourfile\\xxx.m", "-p 'xxxParamName'", "-v true"])
self.assertTrue('xxxParamName.Value = true', result)
if __name__ == '__main__':
unittest.main()
The tests always pass, any idea what I am doing wrong?
Edit I tried doing the same way as this guy did: argparse fails when called from unittest test
def testOneValue(self):
parsed = paramParser.main(["-d","C:\\Path\\to\\yourfile\\xxx.m", "-p", "xxxParamName", "-v", "3"])
self.assertEqual(parsed["d"], "C:\\Path\\to\\yourfile\\xxx.m")
self.assertEqual(parsed["p"], "xxxParamName")
self.assertEqual(parsed["v"], "3")
While his passes (with the help of the [] and seperating the arguments) mine does not. I get this error:
TypeError: 'NoneType' object has no attribute '__getitem__'
Just a quick update about the last issue. I forgot to add a return in my def main(). The tests are now working.