[Qgis-developer] How do QGIS plugin unit test?

BJ Jang qgis.tr.kr at gmail.com
Wed Jul 2 18:54:49 PDT 2014


Dear all.

I want make a QGIS Python plugin with unit test using the 'Plugin Builder'
The 'Plugin Builder' make some unit test class automatically in test folder
like as test_init.py,  test_translations.py.
I want to do unit test by these class but I had failed.

This is my unit test code.

> import unittest
> import MyPlugin
> from MyPlugin.test.test_init import TestInit
> suite = unittest.TestLoader().loadTestsFromTestCase(TestInit)
> unittest.TextTestRunner(verbosity=2).run(suite)


But, execute results is like this.

> Traceback (most recent call last):
>   File "<input>", line 1, in <module>
>   File "C:/Temp/qgisPluginUnitTest.py", line 4, in <module>
>     unittest.TextTestRunner(verbosity=2).run(suite)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\runner.py", line
> 151, in run
>     test(result)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\suite.py", line
> 70, in __call__
>     return self.run(*args, **kwds)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\suite.py", line
> 108, in run
>     test(result)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\case.py", line
> 395, in __call__
>     return self.run(*args, **kwds)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\case.py", line
> 306, in run
>     result.startTest(self)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\runner.py", line
> 55, in startTest
>     self.stream.flush()
> IOError: [Errno 9] Bad file descriptor
> execfile(u'C:/Temp/qgisPluginUnitTest.py'.encode('mbcs'))
> Traceback (most recent call last):
>   File "<input>", line 1, in <module>
>   File "C:/Temp/qgisPluginUnitTest.py", line 5, in <module>
>     unittest.TextTestRunner(verbosity=2).run(suite)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\runner.py", line
> 151, in run
>     test(result)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\suite.py", line
> 70, in __call__
>     return self.run(*args, **kwds)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\suite.py", line
> 108, in run
>     test(result)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\case.py", line
> 395, in __call__
>     return self.run(*args, **kwds)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\case.py", line
> 306, in run
>     result.startTest(self)
>   File "C:\PROGRA~1\QGISVA~1\apps\Python27\lib\unittest\runner.py", line
> 55, in startTest
>     self.stream.flush()
> IOError: [Errno 9] Bad file descriptor


This is a source code of test_init.py

> # coding=utf-8
> """Tests QGIS plugin init."""
> __author__ = 'Tim Sutton <tim at linfiniti.com>'
> __revision__ = '$Format:%H$'
> __date__ = '17/10/2010'
> __license__ = "GPL"
> __copyright__ = 'Copyright 2012, Australia Indonesia Facility for '
> __copyright__ += 'Disaster Reduction'
> import os
> import unittest
> import logging
> import ConfigParser
> LOGGER = logging.getLogger('QGIS')
>
> class TestInit(unittest.TestCase):
>     """Test that the plugin init is usable for QGIS.
>     Based heavily on the validator class by Alessandro
>     Passoti available here:
>     http://github.com/qgis/qgis-django/blob/master/qgis-app/
>              plugins/validator.py
>     """
>     def test_read_init(self):
>         """Test that the plugin __init__ will validate on plugins.qgis.org
> ."""
>         # You should update this list according to the latest in
>         # https://github.com/qgis/qgis-django/blob/master/qgis-app/
>         #        plugins/validator.py
>         required_metadata = [
>             'name',
>             'description',
>             'version',
>             'qgisMinimumVersion',
>             'email',
>             'author']
>         file_path = os.path.abspath(os.path.join(
>             os.path.dirname(__file__), os.pardir,
>             'metadata.txt'))
>         LOGGER.info(file_path)
>         metadata = []
>         parser = ConfigParser.ConfigParser()
>         parser.optionxform = str
>         parser.read(file_path)
>         message = 'Cannot find a section named "general" in %s' % file_path
>         assert parser.has_section('general'), message
>         metadata.extend(parser.items('general'))
>         for expectation in required_metadata:
>             message = ('Cannot find metadata "%s" in metadata source
> (%s).' % (
>                 expectation, file_path))
>             self.assertIn(expectation, dict(metadata), message)
> if __name__ == '__main__':
>     unittest.main()



Please let me know what is wrong.


Regards,
BJ Jang.



-- 

*----------------------------------------------------------*

Open Source GIS Technical Manager / e-Cartographer
BJ Jang

*Gaia3D Inc,*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20140703/6855eedf/attachment.html>


More information about the Qgis-developer mailing list