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 %

 

 

 

 

 

 

posted @ 2020-12-23 16:20  anobscureretreat  阅读(100)  评论(0编辑  收藏  举报