有没有办法指定一条消息Assert.AreEqual(object, object, string),将自定义消息与默认消息相结合?
我有以下代码:
foreach (var testCase in testCases)
{
Assert.AreEqual(testCase.Value, myObj.myMethod(testCase.Key), combinedMessage);
}
Run Code Online (Sandbox Code Playgroud)
除了下面来自 VS 单元测试框架的示例消息之外,我还想指定测试用例键:
Assert.AreEqual failed. Expected:<True>. Actual:<False>.
也许像Failed on the following test case: AB.
我在VS2012中使用MSTest的几个不同的项目遇到了一个反复出现的问题,我的代码覆盖不时地停止工作(看似随机)而是给了我:
生成空结果:没有检测到二进制文件.确保测试运行,加载了所需的二进制文件,具有匹配的符号文件,并且未通过自定义设置排除.有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=253731
我已经检查了显而易见的(它的建议),但似乎无法弄清楚是什么导致它.
这是我的runsettings文件:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage"
uri="datacollector://Microsoft/CodeCoverage/2.0"
assemblyQualifiedName=" Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector,
Microsoft.VisualStudio.TraceCollector,
Version=11.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<ModulePaths>
<Include>
<ModulePath>.*\.dll$</ModulePath>
</Include>
<Exclude>
<ModulePath>.*\.tests.dll</ModulePath>
</Exclude>
</ModulePaths>
<Attributes>
<Exclude>
<Attribute>.*ExcludeFromCodeCoverageAttribute$</Attribute>
<Attribute>.*GeneratedCodeAttribute$</Attribute>
</Exclude>
</Attributes>
<UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
<AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<CollectAspDotNet>False</CollectAspDotNet>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
Run Code Online (Sandbox Code Playgroud) unit-testing mstest code-coverage visual-studio vs-unit-testing-framework
我有一个 CodedUI 测试套件,作为 MSTest 单元测试运行。我在 AssemblyCleanup 中有一个方法,它将通过电子邮件发送存储在外部数据库中的测试运行结果。如何以编程方式确定上次运行会话期间执行了多少个单元测试?如果我们执行的测试少于 10 次,我不想发送电子邮件。这样,我们仅在运行整个 CodedUI 套件时收到电子邮件,而不是在调试测试时收到电子邮件。我们目前有一个必须手动设置的 SendEmail 标志,但我希望以编程方式确定它。
c# unit-testing mstest coded-ui-tests vs-unit-testing-framework
我创建了简单的MS单元测试项目.Assert.Equal(true, true).当我想运行测试时,我收到来自Resharper和Visual Studio的错误.
"单元测试Runner未能运行测试
System.Xml.XmlException:解析EnityName时发生错误.第1行,第17位."
我错过了什么吗
编辑:我无法添加屏幕截图,我无法选择要复制的文本.好的,我会尽我所能写出来的.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.xml.XmlTextReaderImpl.Throw(String res,String arg)
at System.Xml.XmlText.ParseEnityName()
at System.Xml.XmlTextReaderImplparseEnityReference
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextLoader.ParsePartialContent(XmlNode parentNode,String innerxmltext, xmlNodeType nt)
at System.Xml.XmlElement.set_InnerXml(string value)
at Microsoft.VisualStudio.TestPlatform.Utilities.InferSettingHelper.AppendOrModifyChild(XpathNavigator,String nodePatH,String nodeNAME,sTRING INNERxML)
at Microsoft.VisualStudio.TestPlatform.Utilities.InferSettingsHelper.UpdatedRunConfiguration(XPathNavigator, ArchitectureeffectivePlatform,FrameworkVersion effectiveFramework,String resultsDirectory,String solutionDirectory,String binariesRootDirectory)
Microsoft.VisualStudio.TestPlatform.Utilities.InferSettingsHelper.MergeRunSettingsAndFindCompatibleSource()
at JetBrains.ReSharper.UnitTestProvider.MSTest11.MsTest11Runner.Run(IRemoteTaskServer server,String assemblyLocation,String runConfigurationFilename,IUnitTestRun run)
Run Code Online (Sandbox Code Playgroud) 我尝试在Visual Studio 2013中编译单元测试时收到以下错误:
错误1错误C2338:测试编写者必须为您的类类std :: basic_string <wchar_t,struct std :: char_traits <wchar_t>定义ToString <Q*q>的特化,类std :: allocator <wchar_t >> __cdecl Microsoft :: VisualStudio :: CppUnitTestFramework :: ToString <struct HINSTANCE __>(struct HINSTANCE__*).
您可以通过如下所示的测试方法来复制错误:
const std::wstring moduleName = L"kernel32.dll";
const HMODULE expected = GetModuleHandle(moduleName.c_str());
Microsoft::VisualStudio::CppUnitTestFramework::Assert::AreEqual(expected, expected);
Run Code Online (Sandbox Code Playgroud)
有谁知道我需要怎么写这样的专业化ToString?
c++ windows unit-testing vs-unit-testing-framework visual-studio-2013
最近我为我的项目解决方案创建了单元测试方法。当我进行代码分析以找出代码覆盖率时,它显示 82% 的代码覆盖率。
但是当我在 TFS 上检查代码时,构建服务器代码分析报告显示代码覆盖率为 58%。
请问有人可以告诉我他们是否遇到过这个问题或有任何可能的解决方案吗?
code-coverage tfsbuild vs-unit-testing-framework visual-studio-2017
我有一个自定义授权属性如下所示,我正在尝试编写单元测试来测试其功能.
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Authorization != null)
{
// get the Authorization header value from the request and base64 decode it
string userInfo = Encoding.Default.GetString(Convert.FromBase64String(actionContext.Request.Headers.Authorization.Parameter));
// custom authentication logic
if (string.Equals(userInfo, string.Format("{0}:{1}", "user", "pass")))
{
IsAuthorized(actionContext);
}
else
{
HandleUnauthorizedRequest(actionContext);
}
}
else
{
HandleUnauthorizedRequest(actionContext);
}
}
protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized)
{
ReasonPhrase = "Unauthorized"
};
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我尝试测试时,我得到"System.NullReferenceException:对象引用没有设置为对象的实例." 我试图设置actionContext的request.headers.authorization值,但它没有setter.当我尝试模拟HttpActionContext时,它说它无法从模拟HttpActionContext转换为真实的HttpActionContext.下面是我的测试代码
public class HttpBasicAuthorizeAttributeTest
{
private HttpBasicAuthorizeAttribute ClassUnderTest { get; set; } …Run Code Online (Sandbox Code Playgroud) 除Microsoft之外的单元测试框架具有使用属性添加输入参数和预期结果的选项.
例如,
NUnit有
[TestCase(12,4,3)]
Run Code Online (Sandbox Code Playgroud)
和xUnit有
[InlineData(5, 1, 3, 9)]
Run Code Online (Sandbox Code Playgroud)
微软的方法是什么?
.net c# unit-testing visual-studio vs-unit-testing-framework
我在单元测试类中有一堆单元测试.
当我单独运行每一个时,它们都会通过,但是当我同时运行它们时,第一个通过而其余的都通过:
System.ArgumentException:已添加具有相同键的项
谁能告诉我为什么?我需要如何纠正错误?
样品:
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
#endregion
/// <summary>
///A test for SplitTdsName
///</summary>
[TestMethod()]
public void SplitTdsNameTest_SimpleValidName1()
{
string tdsName = "Mr Daniel Trunley";
MemberName expected = new MemberName("Mr", "Daniel", "Trunley");
MemberName actual;
actual = TdsTransformer.SplitTdsName(tdsName);
Assert.AreEqual(expected, actual);
}
/// <summary>
///A test for SplitTdsName
///</summary>
[TestMethod()]
public void SplitTdsNameTest_SimpleValidName2()
{
string tdsName = "Mr Daniel George Trunley";
MemberName expected …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下签名的方法。
Task<string> Post(PartyVM model);
Run Code Online (Sandbox Code Playgroud)
我正在通过使用以下方法来测试上述Post方法来编写单元测试类。
mockPartyManager.Setup(mr => mr.Post(It.IsAny<PartyVM>())).Returns(
(PartyVM target) =>
{
if (target.PartyID.Equals(default(int)))
{
target.Name = "NewP";
target.Status = "ACTIVE";
target.PartyRoleID = msoList.Count() + 1;
partyList.Add(target);
}
else
{
var original = partyList.Where(q => q.PartyID == target.PartyID).Single();
if (original == null)
{
return "Execution failed";
}
original.Name = target.Name;
original.Status = target.Status;
}
return "Execution Successful";
});
this.MockMSOManager = mockPartyManager.Object;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试返回字符串时收到错误消息。
错误 45 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”
我该如何解决这个问题。