.net 使用NUnit和控制台进行单元测试
我一直在玩弄curses sharp库(pdcurses的c#包装器),编写一些单元测试代码来掌握api及其工作原理,我提出了一个问题 我可以使用以下代码从DLL中运行curses sharp(以便nUnit可以对其进行测试):.net 使用NUnit和控制台进行单元测试,.net,unit-testing,console,nunit,pdcurses,.net,Unit Testing,Console,Nunit,Pdcurses,我一直在玩弄curses sharp库(pdcurses的c#包装器),编写一些单元测试代码来掌握api及其工作原理,我提出了一个问题 我可以使用以下代码从DLL中运行curses sharp(以便nUnit可以对其进行测试): bool consoleAllocated = AllocConsole(); if (!consoleAllocated) throw new Exception("Unable to allocate a ne
bool consoleAllocated = AllocConsole();
if (!consoleAllocated)
throw new Exception("Unable to allocate a new console.");
Curses.InitScr();
Stdscr.Add(4, 6, "This is a test title");
Curses.EndWin();
FreeConsole();
AllocConsole和FreeConsole是从内核32导入的extern
我想做的是能够将控制台输出从位置4,6读取到字符串,以便通过编程检查我输入的字符串是否正确输出。例如,为了使用TDD创建一个curses风格的应用程序,能够进行这样的检查是非常重要的
我已经查看了Curses和Stdscr对象(都是Curses-Sharp对象)和Console对象(来自windows库),但还没有找到方法。有人有什么想法吗?我设法找到了一个答案,如果有人感兴趣,我将下面的代码包括在内。它很凌乱,因为我还没有把它清理干净,但它应该作为一个例子来说明如何做到这一点 感谢他们出色的签名集
[DllImport("kernel32", SetLastError = true)]
static extern bool AllocConsole();
[DllImport("kernel32", SetLastError = true)]
static extern bool FreeConsole();
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32", SetLastError = true)]
static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput,
[Out]StringBuilder lpCharacter, uint nLength, COORD dwReadCoord,
out uint lpNumberOfCharsRead);
const int STD_OUTPUT_HANDLE = -11;
[StructLayout(LayoutKind.Sequential)]
struct COORD
{
public short X;
public short Y;
}
[Test]
public void WriteTitle()
{
bool consoleAllocated = AllocConsole();
if (!consoleAllocated)
throw new Exception("Unable to allocate a new console.");
Curses.InitScr();
Stdscr.Add(4, 6, "This is a test title");
Stdscr.Refresh();
IntPtr stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
uint length = 20;
StringBuilder consoleOutput = new StringBuilder((int)length);
COORD readCoord;
readCoord.X = 6;
readCoord.Y = 4;
uint numOfCharsRead = 0;
ReadConsoleOutputCharacter(stdOut, consoleOutput, length, readCoord, out numOfCharsRead);
string outputString = consoleOutput.ToString();
Assert.That(outputString, Is.EqualTo("This is a test title"));
Curses.EndWin();
FreeConsole();
}