D
D
Dmitry Demidov2014-01-22 07:45:13
Python
Dmitry Demidov, 2014-01-22 07:45:13

How to write a doc test for a function?

The first time you need to write a doc test for a small function. Based on what I found on the Internet, I do the following:
1. I write the function code and save it to a file called func_name.py

def func_name(argument):
    #Some code
    return result

2. I write the test to a text file func_name_test.txt:
>>> import ~\func_name.py
>>> func_name(test_argument)
test_result

3. I run the command python -m doctest func_name_test.txt
In the terminal, the error is not defined pops up Tried name 'func_name'-
the same result. What's wrong? I hope at least for a hint in the right direction of thought ... from file_name import *from file_name import func_name

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Demidov, 2014-01-23
@ptitca_zu

Decided as follows:
We write the test (except for import, of course) to the module file immediately after the function declaration. So:

def func_name(argument):
    """
    >>> func_name(test_argument)
    test_result
    """
    #Some code
    return result

At the end we add:
if __name__ == "__main__":
    import doctest
    doctest.testmod()

And run the file with the -v option
python func_name.py -v

D
Dmitry Demidov, 2014-01-23
@ptitca_zu

More details here - habrahabr.ru/post/121162

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question