[GRASS-SVN] r61080 - in sandbox/wenzeslaus/gunittest: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Jun 30 07:49:36 PDT 2014
Author: wenzeslaus
Date: 2014-06-30 07:49:36 -0700 (Mon, 30 Jun 2014)
New Revision: 61080
Modified:
sandbox/wenzeslaus/gunittest/case.py
sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py
Log:
gunittest: add assert for module fail
Modified: sandbox/wenzeslaus/gunittest/case.py
===================================================================
--- sandbox/wenzeslaus/gunittest/case.py 2014-06-30 14:15:34 UTC (rev 61079)
+++ sandbox/wenzeslaus/gunittest/case.py 2014-06-30 14:49:36 UTC (rev 61080)
@@ -365,6 +365,8 @@
# because we want to capture it
module.run()
+ print module.outputs['stdout'].value
+ print module.outputs['stderr'].value
if module.popen.returncode:
# TODO: message format
# TODO: stderr?
@@ -373,10 +375,37 @@
# log these to final report
# TODO: always or only if the calling test method failed?
- module.outputs['stdout'].value
- module.outputs['stderr'].value
+ # in any case, this must be done before self.fail()
+ # module.outputs['stdout'].value
+ # module.outputs['stderr'].value
+ # TODO: should we merge stderr to stdout in this case?
+ def assertModuleFail(self, module, msg=None):
+ if module.run_:
+ raise ValueError('Do not run the module manually, set run_=False')
+ if not module.finish_:
+ raise ValueError('This function will always finish module run,'
+ ' set finish_=None or finish_=True.')
+ if module.stdout_ is None:
+ module.stdout_ = subprocess.PIPE
+ elif module.stdout_ != subprocess.PIPE:
+ raise ValueError('stdout_ can be only PIPE or None')
+ # because we want to capture it
+ if module.stderr_ is None:
+ module.stderr_ = subprocess.PIPE
+ elif module.stderr_ != subprocess.PIPE:
+ raise ValueError('stderr_ can be only PIPE or None')
+ # because we want to capture it
+ module.run()
+ print module.outputs['stdout'].value
+ print module.outputs['stderr'].value
+ if not module.popen.returncode:
+ stdmsg = ('Running <%s> ended with zero (successful) return code'
+ ' when expecting module to fail' % module.get_python())
+ self.fail(self._formatMessage(msg, stdmsg))
+
+
# the following lines are for code coverage
def endcov(cov):
cov.stop()
Modified: sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py 2014-06-30 14:15:34 UTC (rev 61079)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_module_assertions.py 2014-06-30 14:49:36 UTC (rev 61080)
@@ -16,22 +16,26 @@
class TestModuleAssertions(GrassTestCase):
# pylint: disable=R0904
- def test_runModule(self):
- rinfo = Module('r.info', map='elevation', flags='g',
+ def setUp(self):
+ self.rinfo = Module('r.info', map='elevation', flags='g',
stdout_=subprocess.PIPE, run_=False)
- rinfo_wrong = copy.deepcopy(rinfo)
- rinfo_wrong.inputs['map'].value = 'does_not_exists'
+ self.rinfo_wrong = copy.deepcopy(self.rinfo)
+ self.wrong_map = 'does_not_exists'
+ self.rinfo_wrong.inputs['map'].value = self.wrong_map
- self.runModule(rinfo)
- self.assertTrue(rinfo.outputs['stdout'])
- self.assertRaises(CalledModuleError, self.runModule, rinfo_wrong)
+ def test_runModule(self):
+ self.runModule(self.rinfo)
+ self.assertTrue(self.rinfo.outputs['stdout'].value)
+ self.assertRaises(CalledModuleError, self.runModule, self.rinfo_wrong)
def test_assertModule(self):
- rinfo = Module('r.info', map='elevation', flags='g',
- stdout_=subprocess.PIPE, run_=False)
- rinfo_wrong = copy.deepcopy(rinfo)
- rinfo_wrong.inputs['map'].value = 'does_not_exists'
+ self.assertModule(self.rinfo)
+ self.assertTrue(self.rinfo.outputs['stdout'].value)
+ self.assertRaises(self.failureException, self.assertModule, self.rinfo_wrong)
- self.assertModule(rinfo)
- self.assertTrue(rinfo.outputs['stdout'])
- self.assertRaises(self.failureException, self.assertModule, rinfo_wrong)
+ def test_assertModuleFail(self):
+ self.assertModuleFail(self.rinfo_wrong)
+ stderr = self.rinfo_wrong.outputs['stderr'].value
+ self.assertTrue(stderr)
+ self.assertIn(self.wrong_map, stderr)
+ self.assertRaises(self.failureException, self.assertModuleFail, self.rinfo)
More information about the grass-commit
mailing list