Bird
Raised Fist0
PyTesttesting~20 mins

Running PyTest in GitHub Actions - 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 GitHub Actions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this GitHub Actions step running PyTest?
Given this GitHub Actions YAML snippet, what will be the output shown in the logs if all tests pass?
PyTest
steps:
  - name: Run tests
    run: pytest --maxfail=1 --disable-warnings -q
AAll tests skipped
B1 passed, 0 failed, 0 skipped
CSyntaxError: invalid syntax
DNo tests collected
Attempts:
2 left
💡 Hint
PyTest shows a summary line with passed, failed, and skipped tests.
Configuration
intermediate
2:00remaining
Which GitHub Actions YAML snippet correctly installs dependencies and runs PyTest?
Select the YAML snippet that correctly installs Python dependencies from requirements.txt and runs PyTest in GitHub Actions.
A
steps:
  - uses: actions/checkout@v3
  - name: Install dependencies
    run: pip install pytest
  - name: Run tests
    run: pytest
B
steps:
  - uses: actions/checkout@v3
  - name: Setup Python
    uses: actions/setup-python@v4
    with:
      python-version: '3.12'
  - name: Run tests
    run: pytest
C
steps:
  - uses: actions/checkout@v3
  - name: Setup Python
    uses: actions/setup-python@v2
    with:
      python-version: '2.7'
  - name: Run tests
    run: pytest
D
steps:
  - uses: actions/checkout@v3
  - name: Setup Python
    uses: actions/setup-python@v4
    with:
      python-version: '3.12'
  - name: Install dependencies
    run: pip install -r requirements.txt
  - name: Run tests
    run: pytest
Attempts:
2 left
💡 Hint
You need to set up Python 3.12 and install dependencies before running tests.
Troubleshoot
advanced
2:00remaining
Why does this GitHub Actions PyTest step fail with 'ModuleNotFoundError'?
You have this step in your workflow: steps: - uses: actions/checkout@v3 - name: Run tests run: pytest But the logs show 'ModuleNotFoundError: No module named mypackage'. What is the most likely cause?
APyTest is not installed in the runner environment.
BThe checkout action failed to download the code.
CThe Python environment is missing the package dependencies because pip install was not run.
DThe pytest command syntax is incorrect.
Attempts:
2 left
💡 Hint
Check if dependencies are installed before running tests.
🔀 Workflow
advanced
2:00remaining
What is the correct order of steps to run PyTest in GitHub Actions?
Arrange these steps in the correct order to run PyTest successfully in GitHub Actions:
A2,4,1,3
B4,2,1,3
C2,1,4,3
D1,2,4,3
Attempts:
2 left
💡 Hint
You must have code checked out before installing dependencies and running tests.
Best Practice
expert
2:00remaining
Which GitHub Actions configuration best ensures test failures stop the workflow immediately?
You want your GitHub Actions workflow to stop running further steps if PyTest finds any failing tests. Which configuration achieves this?
Arun: pytest --maxfail=1 --disable-warnings -q
Brun: pytest --continue-on-collection-errors
Crun: pytest || true
Drun: pytest --maxfail=0
Attempts:
2 left
💡 Hint
Look for an option that stops after the first failure.

Practice

(1/5)
1. What is the main purpose of running pytest in a GitHub Actions workflow?
easy
A. To automatically test code changes and catch errors early
B. To deploy the application to production
C. To create a backup of the repository
D. To format the code automatically

Solution

  1. Step 1: Understand pytest's role in GitHub Actions CI

    PyTest is a testing tool that checks if code works correctly. GitHub Actions runs tests automatically on code changes to catch errors early.
  2. Final Answer:

    To automatically test code changes and catch errors early -> Option A
  3. Quick Check:

    PyTest in GitHub Actions = automatic testing [OK]
Hint: PyTest in GitHub Actions runs tests automatically [OK]
Common Mistakes:
  • Confusing testing with deployment
  • Thinking PyTest formats code
  • Assuming it creates backups
2. Which of the following is the correct step to install PyTest in a GitHub Actions workflow YAML file?
easy
A. run: apt-get install pytest
B. run: npm install pytest
C. run: pip install pytest
D. run: brew install pytest

Solution

  1. Step 1: Identify the package manager and correct install command for PyTest

    PyTest is a Python package installed using pip. The correct command is run: pip install pytest.
  2. Final Answer:

    run: pip install pytest -> Option C
  3. Quick Check:

    Python packages use pip install [OK]
Hint: Use pip to install Python packages like PyTest [OK]
Common Mistakes:
  • Using npm which is for JavaScript
  • Using apt-get or brew which are system package managers
  • Missing the pip command
3. Given this GitHub Actions step in a workflow YAML file:
  - name: Run tests
    run: pytest tests/

What will happen when this step runs?
medium
A. PyTest will deploy the tests to a server
B. PyTest will install dependencies before running tests
C. PyTest will create a test report file automatically
D. PyTest will run all tests inside the tests/ directory

Solution

  1. Step 1: Analyze the pytest tests/ command and default behavior

    pytest tests/ runs all test files inside the tests/ folder by default but does not install dependencies or deploy code.
  2. Final Answer:

    PyTest will run all tests inside the tests/ directory -> Option D
  3. Quick Check:

    pytest tests/ runs tests in tests/ folder [OK]
Hint: pytest <folder> runs tests in that folder [OK]
Common Mistakes:
  • Thinking PyTest installs dependencies
  • Assuming PyTest deploys code
  • Expecting automatic report file creation
4. You wrote this GitHub Actions step:
- name: Run tests
  run: py.test

But the workflow fails with "command not found" error. What is the likely cause?
medium
A. The command should be pytest not py.test
B. PyTest is not installed before running the tests
C. The tests directory is missing
D. GitHub Actions does not support running tests

Solution

  1. Step 1: Diagnose the command not found error for py.test

    PyTest must be installed first; both pytest and py.test work if installed.
  2. Final Answer:

    PyTest is not installed before running the tests -> Option B
  3. Quick Check:

    Install PyTest before running tests [OK]
Hint: Always install PyTest before running it in workflow [OK]
Common Mistakes:
  • Assuming command spelling is wrong
  • Ignoring PyTest installation step
  • Blaming GitHub Actions for test failures
5. You want to create a GitHub Actions workflow that runs PyTest on Python 3.10 and 3.11 versions. Which YAML snippet correctly sets up the Python versions and runs PyTest?
hard
A. jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: [3.10, 3.11] steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: pip install pytest - name: Run tests run: pytest
B. jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: 3.10 - name: Install dependencies run: pip install pytest - name: Run tests run: pytest - name: Set up Python uses: actions/setup-python@v4 with: python-version: 3.11 - name: Run tests run: pytest
C. jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: [3.10, 3.11] steps: - uses: actions/checkout@v3 - name: Install dependencies run: pip install pytest - name: Set up Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Run tests run: pytest
D. jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: 3.10 - name: Run tests run: pytest

Solution

  1. Step 1: Identify YAML with matrix strategy and correct step order for multi-version testing

    Use strategy.matrix to run on 3.10/3.11; checkout, then setup Python with ${{ matrix.python-version }}, install pytest, run tests.
  2. Final Answer:

    YAML snippet with matrix strategy and correct step order -> Option A
  3. Quick Check:

    Matrix + setup-python + install + run = jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: [3.10, 3.11] steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: pip install pytest - name: Run tests run: pytest [OK]
Hint: Use matrix for versions, setup-python before install and test [OK]
Common Mistakes:
  • Not using matrix for multiple versions
  • Installing PyTest before setting Python version
  • Repeating setup-python steps instead of matrix