V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
SlipStupig
V2EX  ›  Python

Python doctest 结果死活写不到文件里面去

  •  
  •   SlipStupig · Mar 22, 2017 · 1973 views
    This topic created in 3332 days ago, the information mentioned may be changed or developed.

    系统:Ubuntu
    python:2.7.6
    我看别人都能写到文件里面, 我只能输出到终端中,难道我的打开方式有问题
    这个是我看的官方实例,也不行

    
    def factorial(n):
        """Return the factorial of n, an exact integer >= 0.
    
        If the result is small enough to fit in an int, return an int.
        Else return a long.
    
       >>> # comments are ignored
        >>> # comments are ignored
        >>> x = 12
        >>> x
        
        >>> if x == 13:
        ...     print "yes"
        ... else:
        ...     print "no"
        ...     print "NO"
        ...     print "NO!!!"
        ...
    
    
        """
    
        import math
        if not n >= 0:
            raise ValueError("n must be >= 0")
        if math.floor(n) != n:
            raise ValueError("n must be exact integer")
        if n+1 == n:  # catch a value like 1e300
            raise OverflowError("n too large")
        result = 1
        factor = 2
        while factor <= n:
            result *= factor
            factor += 1
        return result
    
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    
    
    4 replies    2017-03-22 14:51:59 +08:00
    JhZ7z587cYROBgVQ
        1
    JhZ7z587cYROBgVQ  
       Mar 22, 2017
    我这边试了一下也是不行啊,会不会是搞错了?

    我看文档里面的例子的第二部分是执行文件里的测试用例而不是输出结果到文件里,两个部分都试验了一下结果都是输出在终端的
    JhZ7z587cYROBgVQ
        3
    JhZ7z587cYROBgVQ  
       Mar 22, 2017
    @SlipStupig 文档我有看,你要不试试在命令行加上“> test.log ” 把标准输出定向到文件?
    SlipStupig
        4
    SlipStupig  
    OP
       Mar 22, 2017
    @jason0916 我关键是想把结果输出到代码里面,方便输出文档
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5672 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 07:37 · PVG 15:37 · LAX 00:37 · JFK 03:37
    ♥ Do have faith in what you're doing.