4

So, this third party created a method that only works well on the main thread (due to it using some GUI functionality)

This means that in order to test it (and other methods that require it), I need to be able to run it from the main thread. The problem? Visual Studio runs the unit tests from Worker Threads, and not the main thread.

So... here is the question: How do I get the main thread, and call the method, from a unit test?

  • 2
    You better rewrite your tests and remove the ui functionality calls. – Oscar Oct 03 '17 at 14:20
  • 1
    What is the GUI functionality being tested? It sounds like this code is tightly coupled with the UI and that coupling needs to be corrected in order to unit test the functionality. Otherwise what you're looking for are automated UI tests, not unit tests. – David Oct 03 '17 at 14:21
  • @Oscar not an option. This method is third party so I can't change it, but still need to test it. – Shany Samuel Benedict Topper I Oct 03 '17 at 14:22
  • Check out this project: https://github.com/TestStack/White for UI testing – mnieto Oct 03 '17 at 14:24
  • 2
    The code doesn't need to run on the "main" thread, it needs to run on a *UI* thread. There's a pretty big difference. You can just construct a message loop as part of the initialization for the relevant tests to *make* the thread a UI thread for the sake of the tests. – Servy Oct 03 '17 at 14:27
  • 2
    @David Even if you're testing code that doesn't manipulate UI controls itself, it's often important for tests to run on a UI thread. For example, if testing something that needs to ensure that the UI *isn't* blocked while it does it's non-UI work, you need to actually set up a message loop before calling the test in order to ensure that it properly lets it continue pumping messages. A lot of people, for example, write their async non-UI code improperly such that it only works correctly when not run from a UI thread, so if the unit tests all run in a non-UI thread you won't catch that. – Servy Oct 03 '17 at 14:30
  • @Servy how do I do that? – Shany Samuel Benedict Topper I Oct 03 '17 at 15:05
  • 1
    @ShanySamuelBenedictTopperI That will depend on the testing framework you're using and the UI platform you're writing code for. – Servy Oct 03 '17 at 15:10

0 Answers0