自托管代理上Databricks单元测试Pytest代码覆盖率报告样式异常及报错问题求助
大家好,我目前在搭建一套基于Databricks的单元测试流水线,用Pytest执行Databricks仓库里的Notebook,同时生成测试结果和代码覆盖率报告。流水线跑在自托管Docker代理池上,镜像已经预装了所有必要的依赖(pytest、databricks-connect这些)。
我的YAML配置核心步骤如下:
- script: | pip install pytest pytest-azurepipelines pytest-cov opencensus opencensus-ext-azure pip install -U databricks-connect==9.1.* pip install databricks-cli apt-get update && apt-get install -y junit cobertura export DATABRICKS_HOST=$(Databricks_URL) export DATABRICKS_TOKEN=$(Databricks_Temp_Token) export DATABRICKS_CLUSTER_ID=$(DATABRICKS_CLUSTER_ID) export DATABRICKS_ORG_ID=$(DATABRICKS_ORG_ID) displayName: 'Load Python dependencies' - checkout: self persistCredentials: true clean: true - script: | python -m pytest --junit-xml=$(Build.SourcesDirectory)/logs/TEST-LOCAL.xml --report-dir=$(Build.SourcesDirectory)/logs/ --cov=$(Build.SourcesDirectory)/tests/ --cov-report=html:$(Build.SourcesDirectory)/logs/ $(Build.SourcesDirectory)/tests/test_*.py --no-coverage-upload displayName: 'Run Pytest with coverage' - task: PublishCodeCoverageResults@1 inputs: codeCoverageTool: 'Cobertura' summaryFileLocation: '$(Build.SourcesDirectory)/coverage.xml' reportDirectory: '$(Build.SourcesDirectory)/logs/' - task: PublishTestResults@2 inputs: testResultsFormat: 'JUnit' testResultsFiles: '**/TEST-LOCAL.xml' publishRunAttachments: true failTaskOnFailedTests: true
现在遇到两个棘手的问题,想请教大家:
问题1:去掉--no-coverage-upload后,自托管代理报错找不到coverage.xml
在微软托管代理上直接跑Pytest命令(不带--no-coverage-upload)完全正常,能生成带样式的覆盖率报告;但换到自托管代理上,会抛出这个错误:
##[error]Unable to process command '##vso[codecoverage.publish codecoveragetool=Cobertura;summaryfile=/agent_works/ado_agent_wrk-nYQREz/ado_agent_wrk-nYQuVz/1/s/coverage.xml;reportdirectory=/agent_works/ado_agent_wrk-nYQREz/ado_agent_wrk-nYQREz/1/s/logs;]' successfully. Please reference documentation...
##[error]File '/agent_works/ado_agent_wrk-nYQREz/ado_agent_wrk-nYQuVz/1/s/coverage.xml' does not exist or is not accessible.
为了绕过这个错误,我被迫加上了--no-coverage-upload参数,这时候Pytest能正常生成覆盖率报告文件(包括index.html、style.css等),但新问题又来了。
问题2:加了--no-coverage-upload后,覆盖率报告样式丢失
虽然报告文件都生成了,也通过PublishCodeCoverageResults@1任务指定了正确的报告目录,但在Azure DevOps里打开报告时,样式完全失效,显示的是没有CSS渲染的原始HTML。我确认过style.css确实存在于指定目录里,但就是不生效。
我现在的疑问:
- 为什么微软托管代理没问题,自托管代理就找不到coverage.xml?
--no-coverage-upload是自托管代理必须的参数吗? - 样式丢失的问题是Azure DevOps阻止了Pytest生成的CSS加载吗?明明文件存在却不渲染,怎么解决?
试过多次调整自托管代理的配置,问题依然存在,希望有遇到过类似情况的朋友能给点思路,谢谢!
备注:内容来源于stack exchange,提问作者Narine

