关于jenkins集成python的单元测试

上一篇 / 下一篇  2020-07-25 20:31:41

最近在研究jenkins的集成,然后想把自己写的python工具也用jenkins集成一下

废话少说,来看结构

 

sparking.py

复制代码
@author: lianying'''classSparking:
    @staticmethod
    def get_num_of_zero(bits_num, key):
        cnt=0whilekey:
            cnt+=1key&= key -1returnbits_num -cnt
    @staticmethod
    def get_num_of_one(bits_num, key):
        cnt=0whilekey:
            cnt+=1key&= key -1returncnt

def main():
    s=Sparking()
    s.get_num_of_zero(32,5)if__name__ =='__main__':
    main()
复制代码

test_sparking.py

复制代码
'''@author: allen'''import pytestfromsparking.sparking import SparkingclassTestSparking(object):  
    @pytest.mark.parametrize("bits_num, key, expected",[
        (8,5,2),
        (16,5,2),
        (32,5,2),
        (64,5,2)
        ])
    def test_get_num_of_one(self, bits_num, key, expected):
        assert Sparking.get_num_of_one(bits_num, key)==expected
        
    @pytest.mark.parametrize("bits_num, key, expected",[
        (8,5,6),
        (16,5,14),
        (32,5,30),
        (64,5,62)
        ])
    def test_get_num_of_zero(self, bits_num, key, expected):
        assert Sparking.get_num_of_zero(bits_num, key)== expected
复制代码

代码上次svn,然后配置jenkins;jenkins的安装就不介绍了,网上一搜一大片;jenkins里先安装好Cobertura Plugin插件,用来显示代码覆盖率的

1、创建job

2、配置svn

3、设置触发器

4、构建 (前提需要安装pytest-cov)

5、构建后展示report

6、保存、应用即可

7、执行结果和报告

  console :

复制代码
Started by user anonymous
Building on masterinworkspace C:\Users\lianying\.jenkins\jobs\test_django\workspace
Updating svn://10.134.28.113/project/sparking at revision '2016-06-08T13:54:59.004 +0800'U         tests\test_sparking.py
At revision28[workspace] $ cmd/c call D:\Dev\tomcat\temp\hudson4704689285977025139.bat

C:\Users\lianying\.jenkins\jobs\test_django\workspace>py.test tests\test_sparking.py --junitxml=pytest_result.xml --cov=sparking --cov-report=xml============================= test session starts =============================platform. win32-- Python2.7.0, pytest-2.9.2, py-1.4.31, pluggy-0.3.1rootdir: C:\Users\lianying\.jenkins\jobs\test_django\workspace, inifile: 
plugins: cov-2.2.1, html-1.8.1collected8items

tests\test_sparking.py ........

 generated xml file: C:\Users\lianying\.jenkins\jobs\test_django\workspace\pytest_result.xml--------------- coverage: platform. win32, python2.7.0-final-0----------------Coverage XML written to file coverage.xml==========================8passedin0.11seconds ===========================C:\Users\lianying\.jenkins\jobs\test_django\workspace>exit0[Cobertura] Publishing Cobertura coverage report...
Publishing Cobertura coverage results...
Cobertura coverage report found.
Recording test results
Finished: SUCCESS
复制代码

报告截图:

PS:补充下在virtualenv下的执行

从第4步开始

4、构建 这里需要先安装pytest-html 和pytest-cov ,然后添加SHELL

复制代码
pip freeze > ./requirements.txtif[ ! -d"venv"]; then
    virtualenv-p /usr/bin/python venv
fi

. venv/bin/activate
pip install-r requirements.txt
cd sparking
py.test tests/test_sparking.py --html=pytest_result.html --cov=sparking --cov-report=xml
复制代码

这里使用了pytest-html生成的报告更好看一些

 

后面基本一致,注意生成报告的路径要配置正确,看下结果



TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2020-08-05  
      1
2345678
9101112131415
16171819202122
23242526272829
3031     

数据统计

  • 访问量: 2133
  • 日志数: 113
  • 建立时间: 2020-06-02
  • 更新时间: 2020-08-03

RSS订阅

Open Toolbar