3

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.

Community
  • 1
  • 1
user825023
  • 45
  • 9
  • 3
    You're asking the test to evaluate if a non-empty string is truthy, which it **always will be**. Have you read the actual documentation for `unittest`? – jonrsharpe Mar 05 '16 at 17:22
  • 2
    The first parameter to `assertTrue` is an object that can be cast as a `bool`, not a Boolean statement as a string (which is what you are supplying). Looks like `assertEqual` might be of more use for the first two. – cdarke Mar 05 '16 at 17:27
  • Look at the `argparse` unittest file for more ideas, `Lib/test/test_argparse.py` - though that's covers more than what your code will need. – hpaulj Mar 05 '16 at 23:52

1 Answers1

2

In the assertTrue statements you are passing a non-empty string which always evaluates as True.

If paramParser.main returns its arguments, you should do something like this:

def testOneIntValue(self):
    result = paramParser.main(["-d C:\\Path\\to\\yourfile\\xxx.m", "-p 'xxxParamName'", "-v 3 "])
    self.assertEqual(result.xxxParamName, 3)   # access xxxParamName here the way you pass it

Also, you might want to read about the unittest module and check out this question - How do you write tests for argparse?.

Community
  • 1
  • 1
Forge
  • 6,538
  • 6
  • 44
  • 64
  • I tried another way by writing like this: `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")` but instead i get `TypeError: 'NoneType' object has no attribute '__getitem__'` This works for another person who(which can be seen in my Edit above) – user825023 Mar 06 '16 at 14:03