我一直想知道单元测试的上下文是什么.在Visual Studio中进行测试似乎有3个选项:
第2点)对我来说非常明显,但我不明白点1)和2)的意思.
谢谢
我想知道是否有任何简单的方法可以使用 MSTests 运行特定类别的测试。我目前有一些单元和集成测试。通常,我只想运行单元测试。
我知道你可以用一个类别标记每个测试方法,但从我所见,它似乎只有在你想创建不同的测试列表时才有用。这似乎没什么用,因为对于每个新测试,我都必须更新该列表。
我也试过将集成测试放在一个项目中,将单元测试放在另一个项目中,但我只能看到 3 个运行测试的选项:解决方案和上下文范围和影响测试,所以我想这没有多大帮助。
你们是怎么做的?
谢谢
我在十多年前的代码中有以下逻辑,我必须编写单元测试.它是一个具体的类,以下逻辑在于ctor.是否有一种很好的方法可以为这样的遗留代码编写单元测试/模拟.我正在使用MSTest/RhinoMocks框架和VS 2010 IDE与.Net framework 4.0
public class SomeClass
{
/// ctor
public SomeClass(XmlNode node)
{
//Step 1: Initialise some private variable based on attributes values from the node
//Step 2: Lot of If , else -if statements ---> something like -
if (/*attributeValue is something*/)
{
// Connect to Db, fetch some value based on the attribute value.
// Again the logic of connecting and fetching is in another concrete class
}
else if (/*attributeValue is somthing else*/)
{
// …Run Code Online (Sandbox Code Playgroud) 如何在VisualStudio 2012中停止正在运行的MSTest单元测试?
这看起来很傻,但我似乎找不到办法.我不小心写了一个单元测试,在某些时候卡住并等待永远.
我正在尝试设置一个测试初始化函数来处理一些基本的数据库设置任务; 这是我的基类:
[ TestClass]
public class BaseTest
{
private SqlConnection sqlConnection;
protected SqlTransaction sqlTransaction;
[TestInitialize ()]
protected void InitialiseConnection()
{
// Set-up sqlTransaction
}
[ TestCleanup ()]
protected void RollbackConnection()
{
// Cleanup
}
Run Code Online (Sandbox Code Playgroud)
然后我就像这样使用它:
[ TestClass]
public class MyTest : BaseTest
{
[ TestMethod ]
public void MyFirstTest()
{
/// Access DB here crashes because sqltransaction is null
Run Code Online (Sandbox Code Playgroud)
基类上的断点显示它没有调用该InitialiseConnection方法.我错过了什么吗?
我的所有单元测试都在Visual Studio 2013中突然消失,我似乎无法在测试资源管理器中发现它们.
我们使用visual studio 2010作为我们的主要环境.我安装了Visual Studio 2013 Express Desktop作为我的首选编辑器,在其中工作,然后在2010年进行最终构建/登记.2010年一切正常.但2013年快递将不再发现测试.它曾经,然后在工作中间停止了变化.这些是mstest测试.
我得到以下测试输出:
------ Discover test started ------
TestTypes\13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b could not be loaded because the TIP could not be instantiated for the following reason(s): System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Tip, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Tip, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String …Run Code Online (Sandbox Code Playgroud) 我在命令行上运行MSTest时出现了一组测试失败的测试.但是,同一套测试在VS2012 IDE中通过.
我验证了在命令行和VS2012 IDE上都执行了相同的测试项目dll.
我通过评论测试方法并观察MSTest报告更新的测试摘要来验证这一点,其中一些其他测试失败,在我评论测试方法之前已经过去了.
有什么建议?
我正在从詹金斯(Jenkins)运行MsTest作为构建步骤,但失败并显示:
[测试] $“ c:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE” /resultsfile:TestResult.trx / noisolation“ / testcontainer:C:\ Program Files(x86)\ Jenkins \ workspace \ Test \ UnitTestProject1 \ bin \ Debug \ UnitTestProject1.dll” 拒绝访问 严重:MSTest命令执行失败 java.io.IOException:无法运行程序“ c:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE”(在目录“ C:\ Program Files(x86)\ Jenkins \ workspace \ Test”中):CreateProcess错误= 5,访问被拒绝
我使用MsTest插件。知道我在做什么错吗?
我有一个mstest项目,在执行时可以正常工作:
dotnet test --logger "trx;LogFileName=Result.trx" --settings tests.runsettings
Run Code Online (Sandbox Code Playgroud)
我还可以使用以下方法构建一个自包含的应用程序:
dotnet publish -c Release -f netcoreapp2.1 --force --self-contained --runtime win-x64
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何从产生的输出中运行测试。
呼唤
dotnet test .\ProjectName.dll --logger "trx;LogFileName=Result.trx" --settings tests.runsettings
Run Code Online (Sandbox Code Playgroud)
出现“错误MSB4025:无法加载项目文件”。
关于如何运行此自包含MSTest-Project的任何提示?
我正在使用.Net Core 2.1编写集成测试我有一个看起来像这样的测试:
private TransactionScope scope;
[TestCleanup]
public void TestCleanup()
{
this.scope.Dispose();
}
[TestInitialize]
public void VerifyUsersHaveBeenSeeded()
{
var transactionOptions = new TransactionOptions {
IsolationLevel = IsolationLevel.ReadCommitted
};
//I've also tried using TransactionScopeOption.Required
this.scope = new TransactionScope(TransactionScopeOption.RequiresNew,
transactionOptions);
}
//Note I am using an MDF file during Testing.
protected AstootContext GetContext()
{
var optionsBuilder = new DbContextOptionsBuilder<AstootContext>();
optionsBuilder.UseSqlServer(this.ASTOOT_CONNECTION_STRING);
var context = new AstootContext(optionsBuilder.Options);
return context;
}
[TestMethod]
public async Task RestEzServiceVerifyUpdate()
{
var context = this.GetContext();
var expectedResult = context.Users.First(); …Run Code Online (Sandbox Code Playgroud) mstest ×10
c# ×5
unit-testing ×5
.net ×2
.net-core ×2
c#-4.0 ×1
jenkins ×1
rhino-mocks ×1
transactions ×1