Bird
Raised Fist0
PyTesttesting~20 mins

Running PyTest in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
PyTest Jenkins Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
PyTest command output in Jenkins pipeline
You run the following PyTest command in a Jenkins pipeline step:

pytest tests/ --junitxml=results.xml

What will be the main effect of this command?
PyTest
pytest tests/ --junitxml=results.xml
APyTest runs tests but fails because '--junitxml' is not a valid option.
BPyTest runs tests and outputs results only to the console, ignoring the XML file.
CPyTest runs tests in the 'tests/' folder and saves test results in XML format to 'results.xml'.
DPyTest runs tests and saves results in JSON format to 'results.xml'.
Attempts:
2 left
💡 Hint
Look for the meaning of the '--junitxml' option in PyTest.
Configuration
intermediate
2:00remaining
Jenkinsfile step to run PyTest and archive results
Which Jenkinsfile snippet correctly runs PyTest tests and archives the JUnit XML test report for Jenkins to display?
A
steps {
  sh 'pytest tests/ --junitxml=results.xml'
  junit 'results.xml'
}
B
steps {
  sh 'pytest tests/'
  archiveArtifacts 'results.xml'
}
C
steps {
  sh 'pytest tests/ --junitxml=results.xml'
  archiveArtifacts 'results.xml'
}
D
steps {
  sh 'pytest tests/ --xml=results.xml'
  junit 'results.xml'
}
Attempts:
2 left
💡 Hint
Jenkins uses the 'junit' step to process JUnit XML reports.
Troubleshoot
advanced
2:00remaining
PyTest test results not showing in Jenkins
You configured Jenkins to run PyTest with '--junitxml=results.xml' and added 'junit "results.xml"' in the pipeline. However, Jenkins shows no test results after the build. What is the most likely cause?
AThe 'junit' step must be called before running PyTest.
BThe 'results.xml' file was not created because PyTest failed before tests ran.
CJenkins cannot read XML files generated by PyTest.
DThe 'junit' step requires the XML file to be named 'test-results.xml'.
Attempts:
2 left
💡 Hint
Check if the XML file exists after the build.
🔀 Workflow
advanced
2:00remaining
Best Jenkins pipeline stage order for PyTest and coverage
You want to run PyTest with coverage reporting in Jenkins and publish both test results and coverage reports. Which pipeline stage order is correct?
APublish coverage report, run PyTest with coverage, then publish test results.
BRun PyTest with coverage, publish coverage report, then publish test results.
CPublish test results, run PyTest with coverage, then publish coverage report.
DRun PyTest with coverage, publish test results, then publish coverage report.
Attempts:
2 left
💡 Hint
Test results must be published after tests run.
🧠 Conceptual
expert
2:00remaining
Why use '--junitxml' option with PyTest in Jenkins?
What is the main reason to use the '--junitxml' option when running PyTest in a Jenkins pipeline?
ATo generate an XML file that Jenkins can parse to display detailed test results and trends.
BTo speed up test execution by running tests in parallel.
CTo automatically fix failing tests during the build.
DTo convert test results into JSON format for Jenkins.
Attempts:
2 left
💡 Hint
Think about how Jenkins shows test reports.

Practice

(1/5)
1. What is the main purpose of running pytest in Jenkins?
easy
A. To monitor server performance during tests
B. To deploy Python applications automatically
C. To write new Python test cases inside Jenkins
D. To automate running Python tests and see results in Jenkins

Solution

  1. Step 1: Understand Jenkins role in testing

    Jenkins automates tasks like running tests without manual effort.
  2. Step 2: Identify pytest's role

    Pytest runs Python tests and generates results for Jenkins to display.
  3. Final Answer:

    To automate running Python tests and see results in Jenkins -> Option D
  4. Quick Check:

    Automation of tests = A [OK]
Hint: Jenkins runs tests automatically to show results [OK]
Common Mistakes:
  • Confusing test running with deployment
  • Thinking Jenkins writes tests
  • Assuming Jenkins monitors server performance
2. Which Jenkins Pipeline command correctly runs pytest and saves results in XML format?
easy
A. sh 'pytest --junitxml=results.xml'
B. sh 'pytest -o junit=results.xml'
C. sh 'pytest --xml=results.xml'
D. sh 'pytest --save=results.xml'

Solution

  1. Step 1: Recall pytest XML output option

    The correct option to save results in XML is --junitxml=filename.
  2. Step 2: Match Jenkins shell command syntax

    Jenkins uses sh to run shell commands, so sh 'pytest --junitxml=results.xml' is correct.
  3. Final Answer:

    sh 'pytest --junitxml=results.xml' -> Option A
  4. Quick Check:

    Correct pytest XML flag = D [OK]
Hint: Use --junitxml=filename to save pytest results [OK]
Common Mistakes:
  • Using wrong pytest flags for XML output
  • Incorrect Jenkins shell command syntax
  • Confusing XML output with other formats
3. Given this Jenkins Pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        sh 'pytest --junitxml=results.xml'
        junit 'results.xml'
      }
    }
  }
}

What will Jenkins display after running this pipeline?
medium
A. Test results summary with passed and failed tests
B. Only console output without test results
C. An error because junit step is missing
D. No output because tests are not run

Solution

  1. Step 1: Understand the sh step

    The sh command runs pytest and generates results.xml with test results.
  2. Step 2: Understand the junit step

    The junit step reads results.xml and shows test results in Jenkins UI.
  3. Final Answer:

    Test results summary with passed and failed tests -> Option A
  4. Quick Check:

    pytest + junit step = test summary shown [OK]
Hint: Use junit step to publish pytest XML results [OK]
Common Mistakes:
  • Forgetting to add junit step to publish results
  • Assuming pytest output alone shows results in Jenkins
  • Confusing console logs with test reports
4. You run this Jenkins Pipeline:
sh 'pytest --junitxml=results.xml'
junit 'results.xml'

But Jenkins shows an error: FileNotFoundError: results.xml not found. What is the likely cause?
medium
A. Jenkins workspace is missing
B. Pytest did not run or failed before creating results.xml
C. The junit step is misspelled
D. The XML file is created but in a different directory

Solution

  1. Step 1: Check if pytest ran successfully

    If pytest fails or does not run, it won't create results.xml.
  2. Step 2: Confirm file existence before junit step

    The junit step needs the XML file; if missing, it errors out.
  3. Final Answer:

    Pytest did not run or failed before creating results.xml -> Option B
  4. Quick Check:

    Missing XML means pytest didn't create it [OK]
Hint: Check pytest success before junit step [OK]
Common Mistakes:
  • Assuming junit step spelling causes file error
  • Ignoring pytest failure logs
  • Not verifying file path correctness
5. You want Jenkins to run pytest only on files changed in a Git branch and publish results. Which approach best fits this requirement?
hard
A. Manually run pytest locally and upload results to Jenkins
B. Run pytest on all files every time and ignore changed files
C. Use Jenkins Pipeline to detect changed files, run pytest on those, then publish with junit
D. Use Jenkins to deploy code without running tests

Solution

  1. Step 1: Detect changed files in Jenkins Pipeline

    Use Git commands or plugins to find changed Python files in the branch.
  2. Step 2: Run pytest only on those changed files and publish results

    Run pytest with paths of changed files, then use junit to show results.
  3. Final Answer:

    Use Jenkins Pipeline to detect changed files, run pytest on those, then publish with junit -> Option C
  4. Quick Check:

    Selective test run + publish = A [OK]
Hint: Detect changed files, run pytest on them, publish results [OK]
Common Mistakes:
  • Running all tests every time wasting resources
  • Skipping automated test runs
  • Not publishing test results in Jenkins