zoukankan      html  css  js  c++  java
  • cherrypy pytest 覆盖,测试代码

    tut12.py

    import random
    import string
    import cherrypy
    
    
    
    
    class StringGenerator(object):
        @cherrypy.expose
        def index(self):
            return "Hello world!"
    
    
        @cherrypy.expose
        def generate(self):
            return ''.join(random.sample(string.hexdigits, 8))
    
    
    
    
    if __name__ == '__main__':
        cherrypy.quickstart(StringGenerator())

    test.py

    import cherrypy
    from cherrypy.test import helper
    from tut12 import StringGenerator
    
    
    class SimpleCPTest(helper.CPWebCase):
        @staticmethod
        def setup_server():
            cherrypy.tree.mount(StringGenerator(), '/', {})
    
    
        def test_index(self):
            self.getPage("/")
            self.assertStatus('200 OK')
        def test_generate(self):
            self.getPage("/generate")
            self.assertStatus('200 OK')

    Output

    macname@MacdeMacBook-Pro cherry % pytest -v test.py 
    ======================================================================================================== test session starts =========================================================================================================
    platform darwin -- Python 3.7.4, pytest-5.1.2, py-1.8.0, pluggy-0.13.0 -- /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
    cachedir: .pytest_cache
    rootdir: /Users/macname/Desktop/cherry
    plugins: ordering-0.6
    collected 3 items                                                                                                                                                                                                                    
    
    test.py::SimpleCPTest::test_generate PASSED                                                                                                                                                                                    [ 33%]
    test.py::SimpleCPTest::test_index PASSED                                                                                                                                                                                      [ 66%]
    test.py::SimpleCPTest::test_gc PASSED                                                                                                                                                                                          [100%]
    
    ========================================================================================================= 3 passed in 0.44s ==========================================================================================================
    macname@MacdeMacBook-Pro cherry %

    Output

    macname@MacdeMacBook-Pro cherry % pytest --cov=tut12 --cov-report term-missing test.py
    ======================================================================================================== test session starts =========================================================================================================
    platform darwin -- Python 3.7.4, pytest-5.1.2, py-1.8.0, pluggy-0.13.0
    rootdir: /Users/macname/Desktop/cherry
    plugins: ordering-0.6, cov-2.8.1
    collected 3 items                                                                                                                                                                                                                    
    
    test.py ...                                                                                                                                                                                                                    [100%]
    
    ---------- coverage: platform darwin, python 3.7.4-final-0 -----------
    Name       Stmts   Miss  Cover   Missing
    ----------------------------------------
    tut12.py      10      1    90%   18
    
    
    ========================================================================================================= 3 passed in 0.54s ==========================================================================================================
    macname@MacdeMacBook-Pro cherry %

  • 相关阅读:
    【漏洞挖掘】攻击对外开放的Docker API接口
    使用密钥认证机制远程登录Linux
    极客时间-左耳听风-程序员攻略开篇-零基础启蒙
    WEBSHELL恶意代码批量提取清除工具
    string替换字符串,路径的斜杠替换为下划线
    Linux下文件的三个时间意义及用法
    记录一次lnmp故障报告
    Centos 7.2编译安装MariaDB-10.0.xx
    win 7 浏览器被篡改小插曲
    【 sysbench 性能基准测试 】
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14179306.html
Copyright © 2011-2022 走看看