zhoujun.g 发表于 2018-1-6 22:29:55

jenkins集成python的单元测试

  最近在研究jenkins的集成,然后想把自己写的python工具也用jenkins集成一下
  废话少说,来看结构
https://images2015.cnblogs.com/blog/937114/201606/937114-20160608140426590-1340378996.png
  sparking.py
  

'''  
@author: lianying
  

'''  
class Sparking:
  
@staticmethod
  
def get_num_of_zero(bits_num, key):
  
cnt = 0
  
while key:
  
cnt += 1
  
key &= key -1
  
return bits_num - cnt
  
@staticmethod
  
def get_num_of_one(bits_num, key):
  
cnt = 0
  
while key:
  
cnt += 1
  
key &= key -1
  
return cnt
  

  
def main():
  
s = Sparking()
  
s.get_num_of_zero(32, 5)
  

  
if __name__ == '__main__':
  
main()
  

  test_sparking.py
  

'''  
@author: allen
  

'''  
import pytest
  
from sparking.sparking import Sparking
  

  
class TestSparking(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
https://images2015.cnblogs.com/blog/937114/201606/937114-20160608141442715-1546532730.png
  3、设置触发器
https://images2015.cnblogs.com/blog/937114/201606/937114-20160608141508152-1809785146.png
  4、构建 (前提需要安装pytest-cov)
https://images2015.cnblogs.com/blog/937114/201606/937114-20160608141528418-1085512135.png
  5、构建后展示report
https://images2015.cnblogs.com/blog/937114/201606/937114-20160608141625543-839673258.png
  6、保存、应用即可
  7、执行结果和报告
  console :
  

Started by user anonymous  
Building on master
in workspace 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 revision
28  
$ 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
-- Python 2.7.0, pytest-2.9.2, py-1.4.31, pluggy-0.3.1  
rootdir: C:\Users\lianying\.jenkins\jobs\test_django\workspace, inifile:
  
plugins: cov
-2.2.1, html-1.8.1  
collected
8 items  

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

  
generated xml file: C:\Users\lianying\.jenkins\jobs\test_django\workspace\pytest_result.xml
  

--------------- coverage: platform win32, python 2.7.0-final-0 ----------------  
Coverage XML written to file coverage.xml
  

========================== 8 passed in 0.11 seconds ===========================  

  
C:\Users\lianying\.jenkins\jobs\test_django\workspace
>exit 0  
Publishing Cobertura coverage report...
  
Publishing Cobertura coverage results...
  
Cobertura coverage report found.
  
Recording test results
  
Finished: SUCCESS
  

  报告截图:
https://images2015.cnblogs.com/blog/937114/201606/937114-20160608142526590-1301093998.png
https://images2015.cnblogs.com/blog/937114/201606/937114-20160608142543871-1852260337.png
  PS:补充下在virtualenv下的执行
  从第4步开始
  4、构建 这里需要先安装pytest-html 和pytest-cov ,然后添加SHELL
  

pip freeze > ./requirements.txt  

if [ ! -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生成的报告更好看一些
https://images2015.cnblogs.com/blog/937114/201608/937114-20160812170631312-1126599787.png
  后面基本一致,注意生成报告的路径要配置正确,看下结果:
https://images2015.cnblogs.com/blog/937114/201608/937114-20160812170439140-735561464.png
https://images2015.cnblogs.com/blog/937114/201608/937114-20160812170508500-1446168961.png
页: [1]
查看完整版本: jenkins集成python的单元测试