You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

测试类中使用pd.read_csv()读取命令行传入路径的CSV文件失败

Fix for Pytest CSV Path Error

Key Issues in Original Code

  • Pytest misinterprets positional arguments: Passing extra positional arguments to pytest makes 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.path usage: The CSV file path doesn't belong in sys.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_addoption hook defines --module-path and --csv-path so pytest recognizes these as configuration parameters, not test items.
  • Autouse fixture: setup_module_path adds the module directory to sys.path once at the start of the test session, ensuring the CustomerSegmentation import works.
  • Dataset fixture: The dataset fixture 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:41:27