测试类中使用pd.read_csv()读取命令行传入路径的CSV文件失败
Fix for Pytest CSV Path Error
Key Issues in Original Code
- Pytest misinterprets positional arguments: Passing extra positional arguments to
pytestmakes it treat them as test files/names, causing the "not found" error. - Early dataset loading: The dataset is loaded when the test class is defined—before pytest parses command line arguments—so the path isn't correctly retrieved at that stage.
- Incorrect
sys.pathusage: The CSV file path doesn't belong insys.path(this list is for module imports, not data files).
Corrected Test Code
import sys import numpy as np import pandas as pd import pytest def pytest_addoption(parser): # Define custom command line options for module and CSV paths parser.addoption("--module-path", action="store", required=True, help="Path to the directory containing training.py") parser.addoption("--csv-path", action="store", required=True, help="Full path to the customer data CSV file") @pytest.fixture(scope="session", autouse=True) def setup_module_path(request): # Add the module path to sys.path to enable importing from training.py module_path = request.config.getoption("--module-path") sys.path.insert(0, module_path) @pytest.fixture(scope="class") def dataset(request): # Load dataset once per test class using the command line-specified CSV path csv_path = request.config.getoption("--csv-path") return pd.read_csv(csv_path, parse_dates=["InvoiceDate"]) # Import after adding module path to sys.path from training import CustomerSegmentation class TestCustomerSegmentation: def test_dataset_loaded(self, dataset): # Verify dataset is successfully loaded assert not dataset.empty, "Dataset should not be empty" # Add your other test methods here, using the dataset fixture as needed
Correct Command to Run
python3 -m pytest test_training.py --module-path "customer-segmentation-v1" --csv-path "customer-segmentation-v1/customer_data.csv"
Explanation
- Custom pytest options: The
pytest_addoptionhook defines--module-pathand--csv-pathso pytest recognizes these as configuration parameters, not test items. - Autouse fixture:
setup_module_pathadds the module directory tosys.pathonce at the start of the test session, ensuring theCustomerSegmentationimport works. - Dataset fixture: The
datasetfixture loads the CSV file after pytest parses command line arguments, guaranteeing the correct path is used. It runs once per test class for efficiency.
内容的提问来源于stack exchange,提问作者MahrukhHassan
相关产品推荐
相关产品推荐

