Bitbucket Pipeline测试文件找不到问题及路径配置咨询
Alright, let's break down why you're hitting this FileNotFoundError and get it sorted. The issue boils down to path resolution differences between your local environment and the Bitbucket Pipeline context—even though the file exists in the pipeline's filesystem, your test code might be looking for it in the wrong place.
First, Let's Diagnose the Root Cause
Your error shows the pipeline is checking /opt/atlassian/pipelines/agent/build/tests/Test_input.csv, which you confirmed exists. The problem is likely how your test code references this file:
- If you're using an absolute path (like hardcoding
/opt/...), that's tied to the pipeline's specific filesystem structure and will break elsewhere (or even if the pipeline's working directory changes). - If you're using a relative path, it might be relative to the project root instead of the
tests/directory where your test files live.
Solutions to Try
1. Make Your Test Code Paths Environment-Agnostic (Best Practice)
The most reliable fix is to anchor your file path to the test file's location, so it works everywhere. Use Python's pathlib module to dynamically get the test directory:
from pathlib import Path import pandas as pd # Or whatever library you're using to read the file # Get the directory where the current test file lives test_dir = Path(__file__).parent # Build the path to Test_input.csv relative to the test file input_file_path = test_dir / "Test_input.csv" # Now read the file df = pd.read_csv(input_file_path)
This way, no matter where your tests run (local machine, Bitbucket Pipeline, or another CI tool), the path will resolve correctly.
2. Adjust the Pipeline's Working Directory
If you want to keep your current test code as-is, you can change the working directory in your pipeline step to the tests/ folder before running pytest. Update your bitbucket-pipelines.yml:
image: python:3.7.3 pipelines: default: - step: script: - pip install -r requirements.txt - pip install swolfpy-inputdata - cd tests && pytest # Switch to tests directory first
This makes any relative paths in your tests relative to the tests/ directory, matching your local setup.
3. Double-Check the File Is Committed to Bitbucket
Just to rule out the obvious: ensure tests/Test_input.csv is actually committed and pushed to your Bitbucket repository. It's easy to forget to add local files to git, which would make them missing in the pipeline even if they exist on your machine.
Final Recommendation
Go with Solution 1—it makes your code resilient to environment changes, so you won't have to tweak pipeline configurations or path references down the line. It's the cleanest, most maintainable fix.
内容的提问来源于stack exchange,提问作者Mojtaba Sardar

