运行 在同一解决方案中针对服务器进行集成测试
Running integration tests against the server in the same solution
我用 NUnit/MSTest 编写集成测试,只是因为它更容易。测试需要与同一解决方案中的 TCP 服务器通信,并且它希望同时调试测试和 TCP 服务器。有没有办法在调试模式下从解决方案启动项目(控制台应用程序)并同时调试测试方法?无论我尝试过什么,VS 都不允许。
不可能同时调试两个程序,但为什么需要 运行 调试模式下的控制台应用程序?只需在不调试的情况下启动它,然后在调试模式下启动集成测试 - 如果您想在某个时候调试控制台应用程序而不是测试方法,您可以启动 visual studio 的第二个实例并将调试器附加到控制台应用程序进程并从那里进行调试.
提示您还可以将调试器附加到程序,以编程方式从代码调用 Debuger.Launch()。
这是编写集成测试时的常见场景。集成测试依赖于另一项服务的启动和 运行。为了处理它,我通常调用进程来启动,例如同一解决方案中的 ConsoleApplication 项目。只需添加一个助手 class 即可调用该过程。
internal class ProcessInvoker
{
/// <summary>
/// Invokes the host process for test service
/// </summary>
public static void InvokeDummyService()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
ProcessStartInfo info = new ProcessStartInfo(Path.Combine(path, "DummyService.exe"));
info.UseShellExecute = true;
info.WorkingDirectory = path;
var process = Process.Start(info);
}
/// <summary>
/// Kills the process of service host
/// </summary>
public static void KillDummyService()
{
Process.GetProcessesByName("DummyService").ToList().ForEach(x => x.Kill());
}
}
现在在 TestInitialize 和 TestCleanup 方法中,我将启动进程并终止相应的进程。
/// <summary>
/// Setup required before the tests of the fixture will run.
/// </summary>
[TestFixtureSetUp]
public void Init()
{
ProcessInvoker.InvokeDummyService();
}
/// <summary>
/// Tear down to perform clean when the execution is finished.
/// </summary>
[TestFixtureTearDown]
public void TearDown()
{
ProcessInvoker.KillDummyService();
}
现在,附上这个调试过程的部分来了。这是一个非常棘手的问题。我从 Visual studio 团队找到了一个 VS Addin 可以自动将子进程附加到当前调试器,但它似乎只适用于“f5”调试。
然后我发现 this SO post 并且它真的很有效。我在此处发布完整的代码形式的答案,几乎没有定制:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using EnvDTE;
namespace Common
{
[ComImport, Guid("00000016-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
[PreserveSig]
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}
public class MessageFilter : IOleMessageFilter
{
private const int Handled = 0, RetryAllowed = 2, Retry = 99, Cancel = -1, WaitAndDispatch = 2;
int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
return Handled;
}
int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
return dwRejectType == RetryAllowed ? Retry : Cancel;
}
int IOleMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
return WaitAndDispatch;
}
public static void Register()
{
CoRegisterMessageFilter(new MessageFilter());
}
public static void Revoke()
{
CoRegisterMessageFilter(null);
}
private static void CoRegisterMessageFilter(IOleMessageFilter newFilter)
{
IOleMessageFilter oldFilter;
CoRegisterMessageFilter(newFilter, out oldFilter);
}
[DllImport("Ole32.dll")]
private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
}
public static class AttachDebugger
{
public static void ToProcess(int processId)
{
MessageFilter.Register();
var process = GetProcess(processId);
if (process != null)
{
process.Attach();
Console.WriteLine("Attached to {0}", process.Name);
}
MessageFilter.Revoke();
}
private static Process GetProcess(int processID)
{
var dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.12.0");
var processes = dte.Debugger.LocalProcesses.OfType<Process>();
return processes.SingleOrDefault(x => x.ProcessID == processID);
}
}
}
注意:您需要从 AddReference -> Extentions
添加 VS 自动化库 EnvDTE
现在在 ProcessInvoker
class 进程启动语句后添加对 AttachDebugger
实用程序 class 的调用。
var process = Process.Start(info);
// Add this after invoking process.
AttachDebugger.ToProcess(process.Id);
当我启动调试测试时,它非常有效。该进程已被调用,附加到 VS 并且能够调试其他进程代码。
检查解决方案中的工作解决方案 here. specially WcfDynamicProxy.Tests。我用Nunit在那里写集成测试。
我用 NUnit/MSTest 编写集成测试,只是因为它更容易。测试需要与同一解决方案中的 TCP 服务器通信,并且它希望同时调试测试和 TCP 服务器。有没有办法在调试模式下从解决方案启动项目(控制台应用程序)并同时调试测试方法?无论我尝试过什么,VS 都不允许。
不可能同时调试两个程序,但为什么需要 运行 调试模式下的控制台应用程序?只需在不调试的情况下启动它,然后在调试模式下启动集成测试 - 如果您想在某个时候调试控制台应用程序而不是测试方法,您可以启动 visual studio 的第二个实例并将调试器附加到控制台应用程序进程并从那里进行调试.
提示您还可以将调试器附加到程序,以编程方式从代码调用 Debuger.Launch()。
这是编写集成测试时的常见场景。集成测试依赖于另一项服务的启动和 运行。为了处理它,我通常调用进程来启动,例如同一解决方案中的 ConsoleApplication 项目。只需添加一个助手 class 即可调用该过程。
internal class ProcessInvoker
{
/// <summary>
/// Invokes the host process for test service
/// </summary>
public static void InvokeDummyService()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
ProcessStartInfo info = new ProcessStartInfo(Path.Combine(path, "DummyService.exe"));
info.UseShellExecute = true;
info.WorkingDirectory = path;
var process = Process.Start(info);
}
/// <summary>
/// Kills the process of service host
/// </summary>
public static void KillDummyService()
{
Process.GetProcessesByName("DummyService").ToList().ForEach(x => x.Kill());
}
}
现在在 TestInitialize 和 TestCleanup 方法中,我将启动进程并终止相应的进程。
/// <summary>
/// Setup required before the tests of the fixture will run.
/// </summary>
[TestFixtureSetUp]
public void Init()
{
ProcessInvoker.InvokeDummyService();
}
/// <summary>
/// Tear down to perform clean when the execution is finished.
/// </summary>
[TestFixtureTearDown]
public void TearDown()
{
ProcessInvoker.KillDummyService();
}
现在,附上这个调试过程的部分来了。这是一个非常棘手的问题。我从 Visual studio 团队找到了一个 VS Addin 可以自动将子进程附加到当前调试器,但它似乎只适用于“f5”调试。
然后我发现 this SO post 并且它真的很有效。我在此处发布完整的代码形式的答案,几乎没有定制:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using EnvDTE;
namespace Common
{
[ComImport, Guid("00000016-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
[PreserveSig]
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}
public class MessageFilter : IOleMessageFilter
{
private const int Handled = 0, RetryAllowed = 2, Retry = 99, Cancel = -1, WaitAndDispatch = 2;
int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
return Handled;
}
int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
return dwRejectType == RetryAllowed ? Retry : Cancel;
}
int IOleMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
return WaitAndDispatch;
}
public static void Register()
{
CoRegisterMessageFilter(new MessageFilter());
}
public static void Revoke()
{
CoRegisterMessageFilter(null);
}
private static void CoRegisterMessageFilter(IOleMessageFilter newFilter)
{
IOleMessageFilter oldFilter;
CoRegisterMessageFilter(newFilter, out oldFilter);
}
[DllImport("Ole32.dll")]
private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
}
public static class AttachDebugger
{
public static void ToProcess(int processId)
{
MessageFilter.Register();
var process = GetProcess(processId);
if (process != null)
{
process.Attach();
Console.WriteLine("Attached to {0}", process.Name);
}
MessageFilter.Revoke();
}
private static Process GetProcess(int processID)
{
var dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.12.0");
var processes = dte.Debugger.LocalProcesses.OfType<Process>();
return processes.SingleOrDefault(x => x.ProcessID == processID);
}
}
}
注意:您需要从 AddReference -> Extentions
添加 VS 自动化库EnvDTE
现在在 ProcessInvoker
class 进程启动语句后添加对 AttachDebugger
实用程序 class 的调用。
var process = Process.Start(info);
// Add this after invoking process.
AttachDebugger.ToProcess(process.Id);
当我启动调试测试时,它非常有效。该进程已被调用,附加到 VS 并且能够调试其他进程代码。
检查解决方案中的工作解决方案 here. specially WcfDynamicProxy.Tests。我用Nunit在那里写集成测试。