Pages

Sunday, October 28, 2012

doctest for python class

You can class test with doctest. One issue to consider is to create object to test the method. This is an example.
"""
This is the "iniFileGenerator" module.

>>> f = iniFileGenerator()
>>> f.get()
hello
"""
class iniFileGenerator:
    def __ini__(self, hintFilePath):
        self.hintFilePath = hintFilePath
    def get(self):
        """
        >>> iniFileGenerator().get()
        hello
        """
        print "hello"
        
if __name__ == "__main__":
    import doctest
    doctest.testmod()
No news is good news, if you want to get the test result you need to use command line.
refactoringChecker> python iniFileGenerator.py -v
Trying:
    f = iniFileGenerator()
Expecting nothing
ok
Trying:
    f.get()
Expecting:
    hello
ok
Trying:
    iniFileGenerator().get()
Expecting:
    hello
ok
2 items had no tests:
    __main__.iniFileGenerator
    __main__.iniFileGenerator.__ini__
2 items passed all tests:
   2 tests in __main__
   1 tests in __main__.iniFileGenerator.get
3 tests in 4 items.
3 passed and 0 failed.
Test passed.

Reuse testing class objects

When testing class, you want to use the same object multiple times throughout the method testing. In this case, you can use extra globs dictionary.
"""
This is the "iniFileGenerator" module.
>>> print f.hintFilePath
./tests/unit_test_files/hint.txt
"""
class iniFileGenerator:
    def __init__(self, hintFilePath):
        self.hintFilePath = hintFilePath
    def hello(self):
        """
        >>> f.hello()
        hello
        """
        print "hello"
if __name__ == "__main__":
    import doctest
    hintFile = "./tests/unit_test_files/hint.txt"
    doctest.testmod(extraglobs={'f': iniFileGenerator(hintFile)})

Reference

23 comments:

  1. The knowledge of python is very essential for the software developers. Python is a high level, general purpose, dynamic programming language that is of code readability and its synatx allows programmers to express the concept in fewer lines of code.
    python training in chennai | python training institutes in chennai

    ReplyDelete
  2. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command .
    Python Training in Chennai

    ReplyDelete
  3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai

    Devops Training in Bangalore

    ReplyDelete
  4. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    python training institute in chennai
    python training in velachery
    python training institute in chennai

    ReplyDelete
  5. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 

    java training in annanagar | java training in chennai


    java training in marathahalli | java training in btm layout

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    SEO company in coimbatore
    SEO Service in Coimbatore
    web design company in coimbatore

    ReplyDelete
  8. Your article is very helpful.You can visit my website:vain glory download

    ReplyDelete
  9. I'm truly enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work! https://python.engineering/25981703-pip-install-fails-with-connection-error-ssl-certificate_verify_failed-certificate-verify-failed-ssl-c-598/

    ReplyDelete