4

I'd like to populate the Forms.Clipboard with text from a NUnit test.

The first problem I encountered was that the Clipboard must be used in STA mode. I found the solution (NUnit 2.5.x+) to set the RequiresSTA attribute on the method.

Next I get an ExternalException when calling Clipboard.SetText(). I found the solution to use Clipboard.SetDataObject(object, bool, int, int) where you can specify retryTimes to make several attempts to set the clipboard. This hacky solution works sometimes, which obviously isn't good enough (increasing the retryTimes parameter will of course also increase the chance of succeeding).

Others (for instance Peter) have succeeded with this, so perhaps it has something to do with my environment: I run VS2010 under Windows 7 64-bit targeting .NET 3.5 and x86. The NUnit version is 2.5.9.

I'm stuck. Any help would be appreciated.

Community
  • 1
  • 1
Torbjörn Kalin
  • 1,976
  • 1
  • 22
  • 31
  • Doesn't sound like a unit test. Have you considered abstracting away from the windows clipboard and using your own "IClipboard" interface while in unit test? – agent-j Jun 10 '11 at 12:22
  • 1
    It is an environmental problem, another process is messing with the clipboard. Use taskmgr.exe and start killing processes until you find the troublemaker. Hopefully that one isn't your own. – Hans Passant Jun 10 '11 at 12:37
  • @agent-j: Couldn't agree more. In this case however Clipboard is part of what I'm testing :( – Torbjörn Kalin Jun 10 '11 at 14:27
  • @Hans Passant: Good thinking. After killing a few apps it seems to be working. I'll do some more testing a report back. – Torbjörn Kalin Jun 10 '11 at 14:30

1 Answers1

0

Answering my own question:

As suggested by Hans Passant in the comments, the problem was that another process was interfering. The evil process seems to be Google Chrome.

Torbjörn Kalin
  • 1,976
  • 1
  • 22
  • 31