許願自動保存單元測試「輸出」,這是通常只通過測試資源管理器窗口中可用,我覺得我有另一種解決方案,如下圖所示:
/// <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 \目錄中。
在單元測試中使用記錄器的目的是什麼? –
如果測試用例文件意味着我不需要簽入代碼。如果我指的是日誌文件意味着我可以清楚的想法 –
你需要用你的測試用例代碼更新你的問題(特別是當你刪除了圖像鏈接時)。它不能被回答。使用記錄器記錄單元測試的結果忽略了測試的重點。 –