【发布时间】:2018-07-22 08:10:01
【问题描述】:
我创建了示例项目 (PyCharm+Mac) 以使用鼻子测试和覆盖率将 SonarQube 集成到 python 中:
src/Sample.py
import sys
def fact(n):
"""
Factorial function
:arg n: Number
:returns: factorial of n
"""
if n == 0:
return 1
return n * fact(n - 1)
def main(n):
res = fact(n)
print(res)
if __name__ == '__main__' and len(sys.argv) > 1:
main(int(sys.argv[1]))
test/SampleTest.py
import unittest
from src.Sample import fact
class TestFactorial(unittest.TestCase):
"""
Our basic test class
"""
def test_fact1(self):
"""
The actual test.
Any method which starts with ``test_`` will considered as a test case.
"""
res = fact(0)
self.assertEqual(res, 1)
def test_fac2(self):
"""
The actual test.
Any method which starts with ``test_`` will considered as a test case.
"""
res = fact(5)
self.assertEqual(res, 120)
if __name__ == '__main__':
unittest.main()
sonar-project.properties
sonar.projectKey=SonarQubeSample
sonar.projectName=Sonar Qube Sample
sonar.projectVersion=1.0
sonar.sources=src
sonar.tests=test
sonar.language=py
sonar.sourceEncoding=UTF-8
sonar.python.xunit.reportPath=nosetests.xml
sonar.python.coverage.reportPath=coverage.xml
sonar.python.coveragePlugin=cobertura
以下命令将成功创建 nosetests.xml 文件:
nosetests --with-xunit ./test/SampleTest.py
当我运行以下命令时:
nosetests --with-coverage --cover-package=src --cover-inclusive --cover-xml
结果如下:
Name Stmts Miss Cover
-------------------------------------
src/Sample.py 10 6 40%
src/__init__.py 0 0 100%
-------------------------------------
TOTAL 10 6 40%
----------------------------------------------------------------------
Ran 0 tests in 0.011s
OK
为什么在运行sonar-scanner 命令后,SonarQube 中的实际功能代码未显示在我的项目中,如下所示?
【问题讨论】:
-
因为您只是在测试
fact,这不是该文件中的代码的 100%?为什么你认为你会得到更多? -
当我检查 coverage.xml 时,它会显示事实代码的命中。
标签: python sonarqube code-coverage nose