[PYTHON] A simple unit testing framework

Source: Internet
Author: User

Recently tried a bit of TDD (test-driven) mode, feel good, in this summary, students if there is a better way, be sure to tell me:)


1. Each function module (file), with a unit test module.

Take this project at hand as an example: There are logcat.py, logmodel.py, scenebuilder.py three modules, then the corresponding new logcattest.py, Logmodeltest, Scenebuildertest.py of three files


2. Each function writes a unit test example accordingly.

For example: There are three functions in logcat.py:

def parsedate (L):
    p = "(? p<date>[0-9]+-[0-9]+) "
    match = Re.search (P, L)
    s = '
    if (match is not None):
        s = match.group (' date ')
    return s

def parsepath (l):
    p = "(? P<path> (\s\s+\/) +\s+) "
    match = Re.search (P, L)
    Path = '
    if (MATC H is not None):
        path = match.group (' path ')
        if Path.startswit H ("assets") is False:
            Path = "assets/" + path
    return path


def parsetime (L):
p = "(? p<time>[0-9]+:[0-9]+:[0-9]+. [0-9]+) "
Match = Re.search (P, L)
t = None
If (match is not None):
s = Match.group (' time ')
T = datetime.datetime.strptime (S, '%h:%m:%s.%f ')
Return T

these functions parse the corresponding variables in a row of logs through regular expressions.

In the logcattest.py, write the corresponding test example:

#LogCatTest. PY
from LogCat import *

logs = []
logs.append (' 09-01 14:36:10.139 E/cocos2d-x Debug Info (23244): File Loaded (4157 bytes): Assets/ui/alpha/hvga/card/card_turn_2.pvr.ccz ')
logs.append (' 09-04 15:41:26.895 D/ Cocos2d-x Debug (16852): File loaded:assets/lang/data/video.txt.dream ')
logs.append (' 09-04 15:41:26.895 D/cocos2d-x Debug (16852): File Loaded:lang/data/video.start.mp3 ')
< Span style= "font-size:13.63636302948px" >logs.append (' 09-04 15:41:26.895 d/cocos2d-x Debug (16852): File loaded:assets /version.txt ')


Def unit_test_parsedate ():
For L in logs:
Print Parsedate (l)

Def unit_test_parsetime ():
For L in logs:
Print Parsetime (l)

Def unit_test_parsepath ():
For L in logs:
Print Parsepath (l)

Of course, these three test cases are similar in function and can be made into one, just to cite an example.


3. In the test module, add the main function, whose function is to dynamically load the test example with command-line arguments:

#LogCatTest. py
Import Traceback
def main ():
Func = sys.argv[1]
UnitTest = "Unit_test_" + func
Globals () [UnitTest] ()


if __name__ = = "__main__":
Try
Main ()
Except Exception as E:
Traceback.print_exc ()
Sys.exit (1)

3. Run at the command line:

Python logcattest.py Parsepath, you can run the appropriate test example.


One thing to note here is that if the test example also reads the parameter from the command line, it needs to start with sys.argv[2], because sys.argv[1] is used as the name of the test example.



[PYTHON] A simple unit test framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.