使用 VS2010 测试工具进行参数化测试

发布于 2024-10-01 07:35:41 字数 331 浏览 1 评论 0原文

是否可以使用 VS2010 Silverlight 测试工具编写参数化测试?

在常规 NUnit 测试中,这可以使用 TestCase 属性来完成...

[Test]
[TestCase("myParam1")]
[TestCase("myParam2")]
[TestCase("myParam3")]
public void TestSomethingWithParameters(string myParam)
{
  // ...some tests using myParam
}

这可以使用 VS2010 测试工具吗?

Is it possible to write parameterized tests using VS2010 Test Tools for Silverlight?

In a regular NUnit test this would be done using TestCase attributes...

[Test]
[TestCase("myParam1")]
[TestCase("myParam2")]
[TestCase("myParam3")]
public void TestSomethingWithParameters(string myParam)
{
  // ...some tests using myParam
}

Is this possible using VS2010 Test Tools?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-10-08 07:35:41

不,这是不可能的。下一个最好的办法是使用数据驱动测试,例如

http: //callumhibbert.blogspot.com/2009/07/data-driven-tests-with-mstest.html

查看 MSDN 文档。

No, this is not possible. The next best thing is to use data driven tests e.g.

http://callumhibbert.blogspot.com/2009/07/data-driven-tests-with-mstest.html

Check out the MSDN documentation also.

夏了南城 2024-10-08 07:35:41

您可以创建一个基类,其中包含测试方法和参数作为虚拟属性。
当您从此类继承时,您只需使用所需的值覆盖属性。
请看示例代码:

public class Operation
{
    public static int Add(int x, int y)
    {
        return x + y;
    }
}

[TestClass]
public class AddTests : WorkItemTest
{
    protected virtual int First{get { return 0; }}
    protected virtual int Second{get { return 0; }}

    [TestInitialize]
    public virtual void Init()
    {
        //Init code
    }

    [TestCleanup]
    public virtual void Clean()
    {
        //Clean code
    }

    [TestMethod]
    [Description("x+y = y+x")]
    public virtual void Test_operation_commutativity()
    {
        Assert.AreEqual(Operation.Add(Second, First), Operation.Add(First, Second));
    }
}

[TestClass]
public class AddPositiveTest : AddTests
{
    protected override int First { get { return 1; } }
    protected override int Second { get { return 2; } }
}

[TestClass]
public class AddNegativeTest : AddTests
{
    protected override int First { get { return -1; } }
    protected override int Second { get { return -2; } }
}

You can create a base class with the test method and the parameters as virtual properties.
When you inherit from this class you only need to override the properties with the desired values.
Please see the sample code:

public class Operation
{
    public static int Add(int x, int y)
    {
        return x + y;
    }
}

[TestClass]
public class AddTests : WorkItemTest
{
    protected virtual int First{get { return 0; }}
    protected virtual int Second{get { return 0; }}

    [TestInitialize]
    public virtual void Init()
    {
        //Init code
    }

    [TestCleanup]
    public virtual void Clean()
    {
        //Clean code
    }

    [TestMethod]
    [Description("x+y = y+x")]
    public virtual void Test_operation_commutativity()
    {
        Assert.AreEqual(Operation.Add(Second, First), Operation.Add(First, Second));
    }
}

[TestClass]
public class AddPositiveTest : AddTests
{
    protected override int First { get { return 1; } }
    protected override int Second { get { return 2; } }
}

[TestClass]
public class AddNegativeTest : AddTests
{
    protected override int First { get { return -1; } }
    protected override int Second { get { return -2; } }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文