最近在研究jenkins的集成,然后想把自己寫的python工具也用jenkins集成一下
廢話少說(shuō),來(lái)看結(jié)構(gòu)
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的安裝就不介紹了,網(wǎng)上一搜一大片;jenkins里先安裝好Cobertura Plugin插件,用來(lái)顯示代碼覆蓋率的
1、創(chuàng)建job
2、配置svn
3、設(shè)置觸發(fā)器
4、構(gòu)建 (前提需要安裝pytest-cov)
5、構(gòu)建后展示report
6、保存、應(yīng)用即可
7、執(zhí)行結(jié)果和報(bào)告
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
[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 -- 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
[Cobertura] Publishing Cobertura coverage report...
Publishing Cobertura coverage results...
Cobertura coverage report found.
Recording test results
Finished: SUCCESS
報(bào)告截圖:
PS:補(bǔ)充下在virtualenv下的執(zhí)行
從第4步開始
4、構(gòu)建 這里需要先安裝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生成的報(bào)告更好看一些
后面基本一致,注意生成報(bào)告的路徑要配置正確,看下結(jié)果:
最后感謝每一個(gè)認(rèn)真閱讀我文章的人,禮尚往來(lái)總是要有的,這些資料,對(duì)于【軟件測(cè)試】的朋友來(lái)說(shuō)應(yīng)該是最全面最完整的備戰(zhàn)倉(cāng)庫(kù),雖然不是什么很值錢的東西,如果你用得到的話可以直接拿走:
這些資料,對(duì)于【軟件測(cè)試】的朋友來(lái)說(shuō)應(yīng)該是最全面最完整的備戰(zhàn)倉(cāng)庫(kù),這個(gè)倉(cāng)庫(kù)也陪伴上萬(wàn)個(gè)測(cè)試工程師們走過(guò)最艱難的路程,希望也能幫助到你!?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-816012.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-816012.html
到了這里,關(guān)于關(guān)于jenkins集成python的單元測(cè)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!