Skip to content

Commit 90917f9

Browse files
committed
DEVOPS-1409: collect coverage data from python tests
- Add coverage.py to test envs - Run tests with coverage.py - Configure sonar to consider python coverage results DEVOPS-1409 (Enable SonarCloud Python code analysis and test coverage)
1 parent 34a866b commit 90917f9

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

.coveragerc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[run]
2+
relative_files = True
3+
branch = True

Jenkinsfile

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ static final String[] WF_TESTS_PYTHON_ENVS = ['env_py36_pa5.yml', 'env_py38_pa7.
88
static final String DEFAULT_WF_TESTS_PYTHON_ENV = 'env_py39_kn47.yml'
99

1010
@groovy.transform.Field
11-
static final String[] PYTEST_PYTHON_ENVS = ['env_py38_legacy', 'env_py38', 'env_py39']
11+
static final List<String> PYTEST_PYTHON_ENVS = ['env_py38_legacy', 'env_py38', 'env_py39']
1212

1313
library "knime-pipeline@$BN"
1414

15-
def baseBranch = (BN == KNIMEConstants.NEXT_RELEASE_BRANCH ? "master" : BN.replace("releases/",""))
15+
def baseBranch = (BN == KNIMEConstants.NEXT_RELEASE_BRANCH ? 'master' : BN.replace('releases/', ''))
1616

1717
/** Return parameters to select python environment to run workflowtests with */
1818
def getWFTestsPythonEnvParameters() {
@@ -54,14 +54,26 @@ try {
5454
String envPath = "${env.WORKSPACE}/pytest-envs/${pyEnv}"
5555
String envYml = "${env.WORKSPACE}/pytest-envs/${pyEnv}.yml"
5656

57-
sh """
58-
micromamba create -p ${envPath} -f ${envYml}
59-
"""
57+
sh(label: 'create conda env', script: """
58+
micromamba create -p ${envPath} -f ${envYml}
59+
""")
6060

61-
sh """
62-
${envPath}/bin/pytest --junit-xml=pytest_results.xml || true
63-
"""
64-
junit "pytest_results.xml"
61+
sh(label: 'run pytest', script: """
62+
${envPath}/bin/coverage run -m pytest --junit-xml=pytest_results.xml || true
63+
64+
# create a separate coverage.xml file for each module
65+
for d in org.knime.python3*/ ; do
66+
${envPath}/bin/coverage xml -o "\${d}coverage-${pyEnv}.xml" --include "*\$d**/*.py" || true
67+
68+
# delete mention of module name in coverage.xml
69+
if [ -f "\${d}coverage-${pyEnv}.xml" ]; then
70+
sed -i "s|\$d||g" "\${d}coverage-${pyEnv}.xml"
71+
fi
72+
done
73+
""")
74+
75+
junit 'pytest_results.xml'
76+
stash(name: "${pyEnv}", includes: "**/coverage-${pyEnv}.xml")
6577
}
6678
}
6779
}
@@ -71,7 +83,7 @@ try {
7183
if (params[env]) {
7284
// need to create a deep copy here, otherwise Jenkins will use
7385
// the last selected option for everything
74-
def environmentFile = new String(env)
86+
String environmentFile = new String(env)
7587
parallelConfigs["${environmentFile}"] = {
7688
runPython3MultiversionWorkflowTestConfig(environmentFile, baseBranch)
7789
}
@@ -82,14 +94,17 @@ try {
8294

8395
stage('Sonarqube analysis') {
8496
env.lastStage = env.STAGE_NAME
85-
workflowTests.runSonar()
97+
env.SONAR_ENV = "Sonarcloud"
98+
configs = workflowTests.ALL_CONFIGURATIONS + PYTEST_PYTHON_ENVS
99+
echo "running sonar on ${configs}"
100+
workflowTests.runSonar(configs)
86101
}
87102
} catch (ex) {
88-
currentBuild.result = 'FAILURE'
89-
throw ex
103+
currentBuild.result = 'FAILURE'
104+
throw ex
90105
} finally {
91-
notifications.notifyBuild(currentBuild.result);
92-
}
106+
notifications.notifyBuild(currentBuild.result)
107+
}
93108

94109
def runPython3MultiversionWorkflowTestConfig(String environmentFile, String baseBranch) {
95110
withEnv([ "KNIME_WORKFLOWTEST_PYTHON_ENVIRONMENT=${environmentFile}" ]) {

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
<feature-branch-extension.branchListProperty>git.branch</feature-branch-extension.branchListProperty>
2020
<feature-branch-extension.appendArtifactoryProperties>false</feature-branch-extension.appendArtifactoryProperties>
2121
<knime.sonar-env>Sonarcloud</knime.sonar-env>
22-
<sonar.python.version>3.9</sonar.python.version>
22+
<sonar.python.version>3.9</sonar.python.version>
23+
<sonar.python.coverage.reportPaths>coverage-*.xml</sonar.python.coverage.reportPaths>
2324
</properties>
2425

2526
<modules>

pytest-envs/env_py38.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- python=3.8
77
- knime-python-base=4.7
88
- pytest
9+
- coverage
910
- black # Formatting test
1011
- matplotlib # View tests
1112
- seaborn # View tests

pytest-envs/env_py38_legacy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ channels:
55
dependencies:
66
- python=3.8
77
- pytest
8+
- coverage
89
- pyarrow=6.0
910
- numpy=1.19
1011
- pandas=1.3

pytest-envs/env_py39.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ channels:
55
dependencies:
66
- python=3.9
77
- knime-python-base=4.7
8+
- coverage
89
- pytest
910
- black # Formatting test
1011
- matplotlib # View tests

0 commit comments

Comments
 (0)