testing method which create a new thread and result we get from event ( NUnit 2.6 )

后端 未结 4 649
别那么骄傲
别那么骄傲 2021-02-10 01:26

I have class which have one public method Start, one private method and one event Finishing. Start call new Thread( private_method )

4条回答
  •  星月不相逢
    2021-02-10 01:39

    You are right.

    First of all, NUnit and its various hosting environments had, or still have, various defects and limitations around threads started from within a test. In particular, if you do not make sure that the thread completes before the test execution is finished, then NUnit has no idea that someone is executing in code that it is going to unload after the test has returned. I remember this pattern regularly causing crashes of VS when NUnit was being executed from it via Resharper integration, as well as occasional glitches and memory leaks of the GUI and console runners provided with NUnit.

    That said, you need to ensure two things.

    1. Elementary safety of the test environment by joining all the threads that you spawn.
    2. Throwing all exceptions that indicate test failure on the main thread only. This means that the background thread has to communicate its results to the main thread, and that one has to Assert all the various invariants.

    Even better, structure your code so that you can unit test it without background threads - if possible.

提交回复
热议问题