关于jenkins集成python的单元测试

打印 上一主题 下一主题

主题 548|帖子 548|积分 1644

最近在研究jenkins的集成,然后想把自己写的python工具也用jenkins集成一下
  废话少说,来看结构
              
           sparking.py
  1. @author: lianying
  2. '''
  3. class Sparking:
  4.     @staticmethod
  5.     def get_num_of_zero(bits_num, key):
  6.         cnt = 0
  7.         while key:
  8.             cnt += 1
  9.             key &= key -1
  10.         return bits_num - cnt
  11.     @staticmethod
  12.     def get_num_of_one(bits_num, key):
  13.         cnt = 0
  14.         while key:
  15.             cnt += 1
  16.             key &= key -1
  17.         return cnt
  18. def main():
  19.     s = Sparking()
  20.     s.get_num_of_zero(32, 5)
  21. if __name__ == '__main__':
  22.     main()
复制代码
test_sparking.py
  1. '''
  2. @author: allen
  3. '''
  4. import pytest
  5. from sparking.sparking import Sparking
  6. class TestSparking(object):  
  7.     @pytest.mark.parametrize("bits_num, key, expected",[
  8.         (8,5,2),
  9.         (16,5,2),
  10.         (32,5,2),
  11.         (64,5,2)
  12.         ])
  13.     def test_get_num_of_one(self, bits_num, key, expected):
  14.         assert Sparking.get_num_of_one(bits_num, key) == expected
  15.         
  16.     @pytest.mark.parametrize("bits_num, key, expected",[
  17.         (8,5,6),
  18.         (16,5,14),
  19.         (32,5,30),
  20.         (64,5,62)
  21.         ])
  22.     def test_get_num_of_zero(self, bits_num, key, expected):
  23.         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 :
  1. Started by user anonymous
  2. Building on master in workspace C:\Users\lianying\.jenkins\jobs\test_django\workspace
  3. Updating svn://10.134.28.113/project/sparking at revision '2016-06-08T13:54:59.004 +0800'
  4. U         tests\test_sparking.py
  5. At revision 28
  6. [workspace] $ cmd /c call D:\Dev\tomcat\temp\hudson4704689285977025139.bat
  7. C:\Users\lianying\.jenkins\jobs\test_django\workspace>py.test tests\test_sparking.py --junitxml=pytest_result.xml --cov=sparking --cov-report=xml
  8. ============================= test session starts =============================
  9. platform win32 -- Python 2.7.0, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
  10. rootdir: C:\Users\lianying\.jenkins\jobs\test_django\workspace, inifile:
  11. plugins: cov-2.2.1, html-1.8.1
  12. collected 8 items
  13. tests\test_sparking.py ........
  14. generated xml file: C:\Users\lianying\.jenkins\jobs\test_django\workspace\pytest_result.xml
  15. --------------- coverage: platform win32, python 2.7.0-final-0 ----------------
  16. Coverage XML written to file coverage.xml
  17. ========================== 8 passed in 0.11 seconds ===========================
  18. C:\Users\lianying\.jenkins\jobs\test_django\workspace>exit 0
  19. [Cobertura] Publishing Cobertura coverage report...
  20. Publishing Cobertura coverage results...
  21. Cobertura coverage report found.
  22. Recording test results
  23. Finished: SUCCESS
复制代码
报告截图:
              
                       
           
  PS:补充下在virtualenv下的执行
  从第4步开始
  4、构建 这里必要先安装pytest-html 和pytest-cov ,然后添加SHELL
  1. pip freeze > ./requirements.txt
  2. if [ ! -d "venv" ]; then
  3.     virtualenv -p /usr/bin/python venv
  4. fi
  5. . venv/bin/activate
  6. pip install -r requirements.txt
  7. cd sparking
  8. py.test tests/test_sparking.py --html=pytest_result.html --cov=sparking --cov-report=xml
复制代码
这里使用了pytest-html生成的报告更好看一些
              
           
  后面基本一致,注意生成报告的路径要配置正确,看下结果:
              
                       
           

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

飞不高

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表