【发布时间】:2019-10-30 14:47:04
【问题描述】:
我创建了一个名为RoleAttribute 的自定义xUnit 理论测试DataAttribute:
public class RoleAttribute : DataAttribute
{
public Role Role { get; set; }
public RoleAttribute(Role role, Action<Role> method)
{
Role = role;
AuthRepository.Login(role);
method(role);
}
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
return new[] { new object[] { Role } };
}
}
而且我有测试方法OpenProfilePageTest:
public class ProfileTest : AuthTest
{
[Theory, Priority(0)]
[Role(Enums.Role.SuperUser, OpenProfilePageTest)]
[Role(Enums.Role.Editor, OpenProfilePageTest)]
public void OpenProfilePageTest(Enums.Role role)
{
var profile = GetPage<ProfilePage>();
profile.GoTo();
profile.IsAt();
}
}
我想要的是它首先执行的每个角色(属性):
AuthRepository.Login(role);(RoleAttribute 的构造函数)
然后继续使用OpenProfilePageTest() 方法中的代码。在它重复相同之前,但对于第二个属性。
我怎样才能做到这一点,现在我正试图通过在属性内传递OpenProfilePageTest() 方法并在其构造函数中执行它来实现这一点。一定有比传递我相信的方法更好的方法来实现这一点吗?
【问题讨论】:
标签: c# .net-core xunit xunit.net xunit2