自动化单元测试的有序测试 [英] ordered test for unit testing in automation
问题描述
我们为自动化编写了一些单元测试。我们希望以某种顺序运行我们的自动化。
We have a few unit tests that we wrote for automation .we would like to run our automation in some order.
在我的Vs 2107版本15.8.7上我没有订购单元测试测试,
On my Vs 2107 version 15.8.7 I don't have ordered test for unit testing,
不再支持了吗?
任何人都可以帮忙吗?
有什么东西吗?否则呢?
Is there something else instead?
请参阅我们正在寻找的屏幕下方的图片:
please see the pic below of the screen that we are looking for:
nir_co
推荐答案
朋友,
感谢您在这里发帖。
我在VS 15.9.6中检查了我的单元测试项目并找到In vs2017,目前我们无法使用单元测试项目的订单测试选项。我也发现同样的问题
这里,似乎MS Test v2不支持有序测试选项,因此产品团队暂时删除了该选项。对于这个问题,
似乎还有很长的路可以实现您的期望,抱歉给您带来不便。
I checked my unit test project in VS 15.9.6 and find In vs2017, currently we can’t use order test option for Unit Test Project. And I also find same issue here, it seems the ordered test option are not supported by MS Test v2 , so the product team remove the option temporarily. For this issue, It seems that there is still a long way to achieve your expectations, sorry for the inconvenience.
并作为替代解决方法,您可以尝试
crokusek 关于它的答案。我的审判如下:
And as an alternative workaround, you can try crokusek’s answer about it. The trial of me are as below:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject18
{
[TestClass]
public class UnitTest1
{
private TestContext testContext;
public TestContext TestContext { get => testContext; set => testContext = value; }
[Priority(100)]
[TestMethod]
public void TestMethod1()
{
OrderedTest.Run(TestContext, new List<OrderedTest>
{
new OrderedTest ( TestMethod3, false ),
new OrderedTest ( TestMethod2, false ),
new OrderedTest ( TestMethod4, true ), // continue on failure
// ...
});
}
[TestMethod]
public void TestMethod2()
{
for (int i = 0; i < 1000000; i++)
{
int a=i;
}
Console.WriteLine(DateTime.Now);
}
[TestMethod]
public void TestMethod3()
{
Console.WriteLine(DateTime.Now);
}
[TestMethod]
public void TestMethod4()
{
Assert.Fail();
Console.WriteLine(DateTime.Now);
}
}
public class OrderedTest
{
/// <summary>Test Method to run</summary>
public Action TestMethod { get; private set; }
/// <summary>Flag indicating whether testing should continue with the next test if the current one fails</summary>
public bool ContinueOnFailure { get; private set; }
/// <summary>Any Exception thrown by the test</summary>
public Exception ExceptionResult;
/// <summary>
/// Constructor
/// </summary>
/// <param name="testMethod"></param>
/// <param name="continueOnFailure">True to continue with the next test if this test fails</param>
public OrderedTest(Action testMethod, bool continueOnFailure = false)
{
TestMethod = testMethod;
ContinueOnFailure = continueOnFailure;
}
/// <summary>
/// Run the test saving any exception within ExceptionResult
/// Throw to the caller only if ContinueOnFailure == false
/// </summary>
/// <param name="testContextOpt"></param>
public void Run()
{
try
{
TestMethod();
}
catch (Exception ex)
{
ExceptionResult = ex;
throw;
}
}
/// <summary>
/// Run a list of OrderedTest's
/// </summary>
static public void Run(TestContext testContext, List<OrderedTest> tests)
{
Stopwatch overallStopWatch = new Stopwatch();
overallStopWatch.Start();
List<Exception> exceptions = new List<Exception>();
int testsAttempted = 0;
for (int i = 0; i < tests.Count; i++)
{
OrderedTest test = tests[i];
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
testContext.WriteLine("Starting ordered test step ({0} of {1}) '{2}' at {3}...\n",
i + 1,
tests.Count,
test.TestMethod.Method,
DateTime.Now.ToString("G"));
try
{
testsAttempted++;
test.Run();
}
catch
{
if (!test.ContinueOnFailure)
break;
}
finally
{
Exception testEx = test.ExceptionResult;
if (testEx != null) // capture any "continue on fail" exception
exceptions.Add(testEx);
testContext.WriteLine("\n{0} ordered test step {1} of {2} '{3}' in {4} at {5}{6}\n",
testEx != null ? "Error: Failed" : "Successfully completed",
i + 1,
tests.Count,
test.TestMethod.Method,
stopWatch.ElapsedMilliseconds > 1000
? (stopWatch.ElapsedMilliseconds * .001) + "s"
: stopWatch.ElapsedMilliseconds + "ms",
DateTime.Now.ToString("G"),
testEx != null
? "\nException: " + testEx.Message +
"\nStackTrace: " + testEx.StackTrace +
"\nContinueOnFailure: " + test.ContinueOnFailure
: "");
}
}
testContext.WriteLine("Completed running {0} of {1} ordered tests with a total of {2} error(s) at {3} in {4}",
testsAttempted,
tests.Count,
exceptions.Count,
DateTime.Now.ToString("G"),
overallStopWatch.ElapsedMilliseconds > 1000
? (overallStopWatch.ElapsedMilliseconds * .001) + "s"
: overallStopWatch.ElapsedMilliseconds + "ms");
if (exceptions.Any())
{
// Test Explorer prints better msgs with this hierarchy rather than using 1 AggregateException().
throw new Exception(String.Join("; ", exceptions.Select(e => e.Message), new AggregateException(exceptions)));
}
}
}
}
它可以通过编码来订购其他测试方法。当我们运行testmethod1时,它将启动其他方法,并且在每个方法的最后我们可以添加一个Console.WriteLine("这是方法xxx"),这样可以帮助我们检查哪个方法通过,哪个方法失败。
It works to order the other test methods by coding. When we run testmethod1, it will start other methods, and in the end of each method we can add a Console.WriteLine("This is method xxx"), so that help us check which method pass and which fail.
希望它有所帮助。任何更新都可以随时与我联系。
Hope it helps. Any update feel free to contact me.
最好的问候
Lance
这篇关于自动化单元测试的有序测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!