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

xUnit测试有执行结果,Allure报告却显示0测试用例问题求助

问题:Allure报告显示“0 test cases”但xUnit测试实际正常执行

我编写了两个检查站点URL可达性的xUnit测试:IsUrlAvailableByHttp执行通过,IsUrlAvailableByHttps执行失败,测试本身运行符合预期。但使用allure generate、allure open命令生成报告后,Allure UI中显示“0 test cases”。

测试代码如下:

public class WebProtocolTest
{
    private readonly PingHelper _pingHelper;

    [AllureBefore("Setup test context")]
    public WebProtocolTest()
    {
        _pingHelper = new PingHelper();
    }

    [Fact]
    [AllureDescription("Check app availability through http")]
    public void IsUrlAvailableByHttp()
    {
        string url = "http://localhost:5001/";

        bool isUrlAvailable = _pingHelper.IsUrlAvailable(url);

        Assert.True(isUrlAvailable);
    }

    [Fact]
    [AllureDescription("Check app availability through https")]
    public void IsUrlAvailableByHttps()
    {
        string url = "https://localhost:5001/";

        bool isUrlAvailable = _pingHelper.IsUrlAvailable(url);

        Assert.True(isUrlAvailable);
    }
}

public class PingHelper
{
    public bool IsUrlAvailable(string url)
    {
        bool urlAvailable = false;

        var request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.AllowAutoRedirect = false;
        request.Method = "HEAD";
        
        try
        {
            var response = request.GetResponse();
            
            if(response != null)
            {
                urlAvailable = true;
            }
        }
        catch (WebException wex)
        {
            
        }

        return urlAvailable;
    }
}

解决方案

1. 确认Allure xUnit集成包安装

确保测试项目已安装兼容版本的Allure.Xunit NuGet包,可通过.csproj文件验证:

<PackageReference Include="Allure.Xunit" Version="2.11.0" /> <!-- 使用与Allure CLI兼容的版本 -->

2. 确保测试执行时生成Allure结果文件

运行测试时需指定结果输出目录,让Allure能捕获测试数据:

dotnet test --results-directory ./allure-results

执行后检查allure-results目录下是否生成了.json格式的测试结果文件,若没有则说明集成未生效。

3. 修正Allure属性的使用方式

xUnit中构造函数上的[AllureBefore]无法被Allure正确识别,需改为单独的Setup方法:

public class WebProtocolTest
{
    private PingHelper _pingHelper;

    [AllureBefore("Setup test context")]
    public void Setup()
    {
        _pingHelper = new PingHelper();
    }

    // 测试方法保持不变...
}

4. 正确执行Allure报告生成命令

确保在包含allure-results目录的路径下执行命令,若结果目录不在当前路径,需显式指定:

allure generate ./allure-results -o ./allure-report
allure open ./allure-report

内容的提问来源于stack exchange,提问作者Oleksandr Kosovan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 18:25:55