这是我在visual studio 2013中的第一个单元测试。 首先,我创建了一个Web应用程序,然后我添加了一个新项目作为单元测试。然后在我的项目中我添加了log4net。 喜欢, Web应用程序webconfig
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--<param name="File" value="LOG/Interview_LogFile.txt"/>-->
<file type="log4net.Util.PatternString" value="%property{LogFileName}" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10000" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<!--<param name="ConversionPattern" value="%date [%thread] : %method %level %logger - %message%newline%exception"/>-->
<param name="ConversionPattern" value="%date [%thread] : %message%newline%exception" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
我的项目文件图片project solution
测试用例Testcase
log4net与Web应用程序一起工作,但单元测试类文件无法正常工作我该怎么办?
答案 0 :(得分:0)
您的记录器配置位于web.config文件中,因此这些设置仅在您的Web应用程序的上下文中可用。
当您运行测试项目时,它具有不同的上下文,并且它不理解您的Web应用程序项目中的web.config文件。
要解决此问题,您需要将app.config文件添加到单元测试项目中,并复制该文件中的记录器设置。
答案 1 :(得分:0)
目前,您有一个单元测试方法,似乎正在测试字符串变量的类型是否为字符串。测试失败的唯一方法是它不是一个字符串。如果发生任何异常,则将其从测试中隐藏,以便测试认为它正确传递。
单元测试框架依赖于抛出异常或断言失败的测试,以便检测失败的测试。因此,您不应该在代码中包含try/catch
,因为这会阻止框架执行其工作。此外,该框架将记录测试失败并向您报告。您不应该尝试使用其他记录器来执行此操作。
让测试工作:
UnitTest1
没有透露测试的目的。try/catch
和logger.***
行。Assert.***
,以有意义的方式测试methodobj
的结果(例如,它报告有效loginvalidation
呼叫成功,无效报告失败)。 答案 2 :(得分:0)
希望自动保存单元测试&#39;输出&#39;通常只能通过Test Explorer窗口获得,我想我有另一个解决方案,如下所示:
/// <summary>
/// Test Logger.
/// </summary>
/// <remarks>
/// Build unit test code as follows:
/// <code>
/// using TestCommon;
/// [TestClass]
/// public class MyTestClass {
///
/// // This property & static MUST be inside your [TestClass]
/// public Microsoft.VisualStudio.TestTools.UnitTesting.TestContext TestContext
/// { get { return context; }
/// set { context = value; } }
/// private static TestContext context = null;
/// private static TestLogger log = null;
/// [ClassInitialize]
/// public void Initialize() {
/// log = new TestLogger(logFileName); // provide a path\name
/// }
///
/// [TestMethod]
/// public void MyTest()
/// {
/// try {
/// log.Open(context);
/// ... // perform a test
/// log.Info("time: {0} Iteration {1}", Now, i);
/// } catch (AssertFailedException ex)
/// log.Exception(ex);
/// throw;
/// } finally {
/// log.Close();
/// }
/// }
/// </code>
/// </remarks>
public class TestLogger
{
private static TestContext context = null;
private string logfilename = null;
private TextWriterTraceListener writer = null;
private DataCollectorMessageLevel errorLevel = DataCollectorMessageLevel.Info;
/// <summary>
/// Microsoft.VisualStudio.TestTools.Common.DataCollectorMessageLevel
/// Levels: Error, Warning, Info (default), Data
/// </summary>
public DataCollectorMessageLevel ErrorLevel
{
get { return errorLevel; }
set { errorLevel = value; }
}
/// <summary>
/// Create the logger. Set up work in Class initializer, it should also work inside each test.
/// </summary>
/// <param name="logfile">Path to the file to log into</param>
public TestLogger(string logfile, bool update = true)
{
logfilename = logfile;
errorLevel = DataCollectorMessageLevel.Info;
if (!update)
try { System.IO.File.Delete(logfile); } catch { }
writer = new TextWriterTraceListener(logfilename);
Debug.Listeners.Add(writer);
Debug.AutoFlush = true;
if (context != null)
{
Debug.WriteLine("Test Logger: {0} - {1}", context.FullyQualifiedTestClassName, context.TestName);
}
}
/// <summary>
/// At start of a test, it logs that information
/// </summary>
/// <param name="theContext">records context to access Test Name</param>
public void Open(TestContext theContext)
{
context = theContext;
Debug.WriteLine("Test Logger: {0} - {1}", context.FullyQualifiedTestClassName, context.TestName);
}
/// <summary>
/// At end of a test. Best if put in a finally clause.
/// </summary>
public void Close()
{
if (context != null)
{
Debug.WriteLine("Test Ending: {0} - {1}", context.FullyQualifiedTestClassName, context.TestName);
}
Debug.WriteLine("");
}
/// <summary>
/// The Logger functions use a standard string format
/// </summary>
/// <param name="format"></param>
/// <param name="parameters"></param>
public void Data(string format, params object[] parameters)
{
if (errorLevel > DataCollectorMessageLevel.Data) return;
Debug.WriteLine(format, parameters);
}
public void Info(string format, params object[] parameters)
{
if (errorLevel > DataCollectorMessageLevel.Info) return;
Debug.WriteLine(format, parameters);
}
public void Warning(string format, params object[] parameters)
{
if (errorLevel > DataCollectorMessageLevel.Warning) return;
Debug.WriteLine(format, parameters);
}
public void Error(string format, params object[] parameters)
{
if (errorLevel > DataCollectorMessageLevel.Error) return;
Debug.WriteLine(format, parameters);
}
/// <summary>
/// Able to include the Assert error message in the log
/// </summary>
/// <param name="ex">ex.Message is the Assert message, ex.ToString includes the call stack</param>
public void Exception(Exception ex)
{
if (ex is AssertFailedException)
{
// ex.Message is only the Assertion text
Debug.WriteLine(String.Format("{0}", ex.ToString()));
}
else
{
Debug.WriteLine(string.Format("{0}", ex.ToString()));
}
}
}
如果您运行或调试测试,我只希望先前知道上下文,以便输出可以放在\ TestResults \目录中。