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 %

  • 相关阅读:
    当公有云Azure拥抱Docker容器技术
    .NET AJAX实例
    漫谈Ajax在.Net中的使用
    .NET运用AJAX 总结及其实例
    Excel自动从身份证中提取生日、性别、年龄
    ASP.NET 与 Ajax 的实现方式
    windows下编辑器Emacs的安装与配置
    2013.10.26工作Fighting(1)
    Jquery操作下拉框(DropDownList)实现取值赋值
    js调用后台,后台调用前台等方法总结
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14179306.html
Copyright © 2011-2022 走看看