Python Forum
How would you test this module?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would you test this module?
#1
How can I write unit tests for this module. I know very little about writing tests.

#-*-coding:utf8;-*-
#qpy:3
#qpy:console



SINGLETONS = [
    'area',
    'base',
    'br',
    'col',
    'command',
    'embed',
    'hr',
    'img',
    'input',
    'keygen',
    'link',
    'meta',
    'param',
    'source',
    'track',
    'wbr'
]
    
       
def tagify(tagname, data='', **kw):
    attrs = ''
    retval = ''
    for key, value in zip(kw.keys(), kw.values()):
        if key == 'cls':
            key = 'class'
            
        if key == '_id':
            key = 'id'
            
        attrs += '{}="{}"'.format(key, value)
        
    if not attrs:
        opentag = '<{}>'.format(tagname)
    else:
        opentag = '<{} {}>'.format(tagname, attrs)
        
    if not tagname in SINGLETONS:
        closetag = '</{}>'.format(tagname)
    else:
        closetag = None
        
    if not closetag:
        retval = '{}'.format(opentag)
  
    if data:
        retval = '{}{}{}'.format(opentag, data, closetag)
    else:
        retval = '{}{}'.format(opentag, closetag)
    return retval

def tag(tagname, **deco_kw):
    def deco(func):
        def wraps(*args, **kw):
            data = func(*args, **kw)
            return tagify(tagname, data, **deco_kw)
        return wraps
    return deco
Reply
#2
In the standard lib, there's unit tests: https://docs.python.org/3/library/unittest.html
and doc tests: https://docs.python.org/3/library/doctest.html

Doc tests are useful if you have a tool that builds documentation for you (such as Sphinx http://www.sphinx-doc.org/en/stable/), so you know your examples are still accurate. Unit tests are probably more useful in general, though.
Reply
#3
Here is the starting point of a testing file, assuming the initial file is tagfoo.py
# file testtagfoo.py
import unittest as ut
from tagfoo import tagify, tag

class TestTagify(ut.TestCase):
    def test_simple_image_tag(self):
        rv = tagify('image', 'An Image', src='filename.jpg')
        self.assertEqual(
            rv,
            '<image src="filename.jpg">An Image</image>')
        
if __name__ == '__main__':
    ut.main()
You can add methods test_... to the TestTagify class, and create new classes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,144 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,132 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,569 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020