Framework Mode - Running PyTest in Jenkins
Folder Structure for PyTest Project
project-root/ ├── tests/ │ ├── test_example.py │ └── conftest.py ├── src/ │ └── application_code.py ├── pytest.ini ├── requirements.txt └── Jenkinsfile
Jump into concepts and practice - no test required
project-root/ ├── tests/ │ ├── test_example.py │ └── conftest.py ├── src/ │ └── application_code.py ├── pytest.ini ├── requirements.txt └── Jenkinsfile
tests/ folder, contains test cases using PyTest syntax.conftest.py for setup and teardown reusable code.src/ folder, the code under test.pytest.ini for PyTest settings and markers.Jenkinsfile defines Jenkins build and test steps.--junitxml=reports/result.xml), and test paths.pipeline {
agent any
stages {
stage('Install') {
steps {
sh 'python -m venv venv'
sh './venv/bin/pip install -r requirements.txt'
}
}
stage('Test') {
steps {
sh './venv/bin/pytest --junitxml=reports/result.xml'
}
}
}
post {
always {
junit 'reports/result.xml'
archiveArtifacts artifacts: 'reports/**', allowEmptyArchive: true
}
}
}
result.xml for Jenkins to parse test results.Where in this framework structure would you add a new fixture to set up a database connection for tests?
pytest in Jenkins?--junitxml=filename.sh to run shell commands, so sh 'pytest --junitxml=results.xml' is correct.pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'pytest --junitxml=results.xml'
junit 'results.xml'
}
}
}
}sh stepsh command runs pytest and generates results.xml with test results.junit stepjunit step reads results.xml and shows test results in Jenkins UI.sh 'pytest --junitxml=results.xml' junit 'results.xml'
FileNotFoundError: results.xml not found. What is the likely cause?results.xml.junit step needs the XML file; if missing, it errors out.pytest with paths of changed files, then use junit to show results.junit -> Option C