我有很多以这种格式编写的测试:
[TestMethod]
public void TestMethod1()
{
try
{
DoShomething();
}
catch (Exception e)
{
WriteExceptionLogWithScreenshot( e );
}
}
[TestMethod]
public void TestMethod2()
{
try
{
DoAnotherShomething();
}
catch ( Exception e )
{
WriteExceptionLogWithScreenshot( e );
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用类似的东西来统一这个异常处理
[TestCleanup]
public void Cleanup()
{
// find out if an exception was thrown and run WriteExceptionLogWithScreenshot( e )
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以避免在所有方法中编写 try catch 块。
mstest 支持这样的东西吗?有人知道我能做什么吗?
我想通过 MS Test(从 Windows 控制台)运行单元测试,只要失败的测试计数超过特定阈值,我就可以停止/终止测试执行。
对于我的用例,当一定比例的测试已经失败时,没有必要继续运行测试。
我只能考虑创建一个新的控制台应用程序来包装 mstest.exe 执行,这样我就可以实时解析标准输出,并最终终止该进程,例如:
var pi = new ProcessStartInfo()
{
FileName = MS_TEST,
UseShellExecute = false,
RedirectStandardOutput = true,
Arguments = MS_TEST_ARGS
};
int passed = 0; int failed = 0;
using (var process = Process.Start(pi))
{
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
if (line.Contains("Passed"))
passed++;
if (line.Contains("Failed"))
failed++;
if (failed >= THRESHOLD)
{
process.Kill();
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
谁能提出更好的方法来做到这一点?我认为 MsTest 本身并不支持这一点。
PowerShell 似乎是一个选项,但标准输出重定向并不是微不足道的。
请注意,我无法修改测试代码,我需要在不以任何方式修改测试代码的情况下完成此操作。
如果预期变量是整数,它就像这样
[DataRow(2)]
[TestMethod]
public void TestMethod(int expected)
{
// some code...
}
Run Code Online (Sandbox Code Playgroud)
但是当有二维数组 int[,] 而不是 int 参数时该怎么办呢?当我尝试这样做时
[DataRow(new int[,] { {0, 0}, {0, 0} })]
[TestMethod]
public void TestMethod(int[,] expected)
{
// some code...
}
Run Code Online (Sandbox Code Playgroud)
错误说
属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式
c# unit-testing mstest data-driven-tests parameterized-unit-test
我们有一些 UI 集成测试无法在构建服务器上运行,因为启动测试 GUI 应用程序需要以用户身份运行构建代理(而不是当前设置的服务)。
这会导致构建管道卡住。所以我想在本地运行这些测试,而不是在构建服务器上。
有没有办法使用xUnit或MSTests和Azure DevOps构建管道来实现此目的?
我们正在使用 DevOps 来构建我们的 .net 4.7.2 应用程序。作为其中的一部分,我们正在运行使用 nunit 框架和测试运行器的单元测试。
它已经正常运行了大约 18 个月,但在最后一天刚刚停止工作:(
它使用标准模板来运行测试,如下所示:
- task: VSTest@2
displayName: "Running tests"
inputs:
testSelector: 'testAssemblies'
testAssemblyVer2: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
searchFolder: '$(System.DefaultWorkingDirectory)'
Run Code Online (Sandbox Code Playgroud)
但是,现在该步骤失败并显示以下日志:
NUnit 适配器 4.2.0.0:测试执行开始运行 D:\a\1\s\Configuration.Tests\bin\Release\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中的所有测试 NUnit3TestExecutor 使用 Current Discovery 发现 0 个 NUnit 测试用例中的 0 个模式,显式运行 运行 D:\a\1\s\Configuration.Tests\bin\Release\testcentric.engine.metadata.dll 中的所有测试 NUnit3TestExecutor 使用当前发现模式发现 0 个 NUnit 测试用例中的 0 个,显式运行 运行所有测试在 D:\a\1\s\Api.Tests\bin\Release\testcentric.engine.metadata.dll 中,NUnit3TestExecutor 使用当前发现模式发现了 0 个 NUnit 测试用例中的 0 个,显式运行 在 D:\a\1 中运行所有测试\s\CommunicationTests\bin\Release\testcentric.engine.metadata.dll NUnit3TestExecutor 使用当前发现模式发现 0 个 NUnit 测试用例中的 0 个,显式运行 运行 D:\a\1\s\Domain.Tests\bin\ 中的所有测试Release\testcentric.engine.metadata.dll …
我有一个.net测试类.在Initialize方法中,我创建了一个windsor容器并进行了一些注册.在实际的测试方法中,我在控制器类上调用一个方法,但是拦截器不起作用,并且直接调用该方法.这有什么潜在的原因?
这是所有相关代码:
test.cs中:
private SomeController _someController;
[TestInitialize]
public void Initialize()
{
Container.Register(Component.For<SomeInterceptor>());
Container.Register(
Component.For<SomeController>()
.ImplementedBy<SomeController>()
.Interceptors(InterceptorReference.ForType<SomeInterceptor>())
.SelectedWith(new DefaultInterceptorSelector())
.Anywhere);
_someController = Container.Resolve<SomeController>();
}
[TestMethod]
public void Should_Do_Something()
{
_someController.SomeMethod(new SomeParameter());
}
Run Code Online (Sandbox Code Playgroud)
SomeController.cs:
[HttpPost]
public JsonResult SomeMethod(SomeParameter parameter)
{
throw new Exception("Hello");
}
Run Code Online (Sandbox Code Playgroud)
SomeInterceptor.cs:
public class SomeInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// This does not gets called in test but gets called in production
try
{
invocation.Proceed();
}
catch
{
invocation.ReturnValue = new SomeClass();
}
}
} …Run Code Online (Sandbox Code Playgroud) Visual Studio 2010为我生成了一堆单元测试,这似乎很方便,但我怀疑它们是否是实际的单元测试.例如,它生成了一个名为SaveTest的测试,它执行以下代码:
User user = new User(); //I have to create a user
user.Save(); //This saves the user to the database.
//Assertions here....
Run Code Online (Sandbox Code Playgroud)
我上面的问题是,从我所读到的,单元测试应该在隔离中测试,所以通过测试保存到数据库,这不是单元测试或我错了,MsTest是否错了?作为旁注,User由dbml和Save calles SubmitChanges在我的DataContext上生成.
我曾经比较像这样的列表,但它在测试中返回false:
Assert.IsTrue(expected.SequenceEquals(actual));
Run Code Online (Sandbox Code Playgroud)
并尝试转换为json,它工作:
Assert.AreEqual(expected.ToJson(), actual.ToJson());
Run Code Online (Sandbox Code Playgroud)
价值似乎是平等的,可能会有什么不同?如何找出列表中的不同之处?
更新:
我的课:
public class Department
{
[BsonId]
public ObjectId Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Id.ToString();
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将我的某些WebDriver测试从JAVA移植到C#。我坚持的是驱动程序无法在页面上找到某些元素的情况,在JAVA中,我这样做:
if (second >= 10) fail("timeout - " + list);
Run Code Online (Sandbox Code Playgroud)
因此,如果某件事花费了10秒钟以上,则测试将失败并显示超时消息。我在C#中尝试了类似的方法
if (sec >= 10) Debug.Fail("timeout : " + vList);
Run Code Online (Sandbox Code Playgroud)
但这实际上并没有使测试失败,但是给了我一个选择,它带有异常消息框。那不是不,我需要我的自动测试完全失败。然后我尝试
if (sec >= 10) Assert.Fail("timeout : " + vList);
Run Code Online (Sandbox Code Playgroud)
但这会引发未处理的异常错误。我应该在try / catch块中包含Assert.Fail吗?还是应该使用完全不同的方法使测试失败?
如主题中所述,我正在使用MSTest。
编辑:确切的消息是:
用户代码未处理AssertFailedException。断言失败。超时:someField。
上
Assert.Fail(“ timeout:” + vList);
美好的一天!
我有以下项目结构:
Solution
-My.Project
-My.Project.Test
--Resources
---mytest.file
Run Code Online (Sandbox Code Playgroud)
在我的测试文件中跟随方法:
[TestMethod]
[DeploymentItem("My.Project.Test\\Resources\\mytest.file", @"Resources")]
TestMethod()
{
....
string path = "Resources\\mytest.file"; ....
}
Run Code Online (Sandbox Code Playgroud)
根据http://msdn.microsoft.com/en-us/library/ms182475%28v=vs.100%29.aspx,我想只使用DeployItemAttribute而不使用"部署项属性".
但是当我调试它的方法时,我抓住了"File not found exception".
我能做什么?
PS:还在http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/474d51ee-7ab9-4311-b7b0-b21266eb01d5上发布了这个问题
最好的祝福!
mstest ×10
c# ×6
unit-testing ×4
azure-devops ×2
.net ×1
.net-5 ×1
devops ×1
interceptor ×1
nunit ×1
powershell ×1
webdriver ×1
xunit ×1