AllureFeature在多NUnit测试用例中重复显示问题求助
问题:Allure报告无法按Feature维度正确拆分测试用例
我希望在不同NUnit测试用例中通过AllureFeature特性区分测试维度(配送方式、产品、附加费),但生成的Allure报告里每个Feature都包含了所有测试用例的内容。以下是税务规则相关的测试代码示例,期望报告能按配送方式、税务规则等维度拆分展示对应测试:
namespace Tests.Ecommerce { [TestFixture] [AllureNUnit] [AllureEpic("Ecommerce")] [AllureFeature("TaxRule")] public class TaxRules : EcommerceBase { [TestCase("ProductVariant")] [TestCase("Shipping"), AllureFeature("Shipping")] [TestCase("Surcharge"), AllureFeature("Surcharge")] public static void MyRandomTest(string feature) { // 测试逻辑 } } }
问题根源
类级别标注的[AllureFeature("TaxRule")]会被所有测试用例继承,导致每个测试用例同时归属到类的TaxRule Feature和TestCase上指定的Feature(如Shipping、Surcharge),最终报告中每个Feature都会显示所有测试用例。
解决方案
方案1:移除类级别Feature,按TestCase单独指定Feature
如果需要将配送方式、产品、附加费作为独立的Feature展示,直接移除类上的[AllureFeature("TaxRule")],给每个TestCase明确标注对应的Feature:
namespace Tests.Ecommerce { [TestFixture] [AllureNUnit] [AllureEpic("Ecommerce")] public class TaxRules : EcommerceBase { [TestCase("ProductVariant"), AllureFeature("ProductVariant")] [TestCase("Shipping"), AllureFeature("Shipping")] [TestCase("Surcharge"), AllureFeature("Surcharge")] public static void MyRandomTest(string feature) { // 测试逻辑 } } }
方案2:用AllureStory实现层级拆分
如果需要保留TaxRule作为顶级Feature,将配送方式、产品、附加费作为其下的子分类(Story),可以保留类级别Feature,改用AllureStory标记每个测试用例的维度:
namespace Tests.Ecommerce { [TestFixture] [AllureNUnit] [AllureEpic("Ecommerce")] [AllureFeature("TaxRule")] public class TaxRules : EcommerceBase { [TestCase("ProductVariant"), AllureStory("ProductVariant")] [TestCase("Shipping"), AllureStory("Shipping")] [TestCase("Surcharge"), AllureStory("Surcharge")] public static void MyRandomTest(string feature) { // 测试逻辑 } } }
方案3:动态修改Feature(覆盖类级别设置)
如果必须保留类级别Feature,又需要每个测试只归属一个Feature,可以在测试方法内部通过Allure API动态清除并设置当前Feature:
using Allure.Net.Commons; namespace Tests.Ecommerce { [TestFixture] [AllureNUnit] [AllureEpic("Ecommerce")] [AllureFeature("TaxRule")] public class TaxRules : EcommerceBase { [TestCase("ProductVariant")] [TestCase("Shipping")] [TestCase("Surcharge")] public static void MyRandomTest(string feature) { // 清除继承的类级别Feature,设置当前测试的Feature AllureLifecycle.Instance.UpdateTestCase(test => { test.Features.Clear(); test.Features.Add(feature); }); // 测试逻辑 } } }
内容的提问来源于stack exchange,提问作者Justin Sletcher
相关产品推荐
相关产品推荐

