mercredi 26 juin 2019

C#: How to #define within an #if?

I have a certain method that runs as a background task and does some fancy things. It is SLOW, because of the Thread.Sleep(500);. It doesn't need to do things faster, and it changes the behaviour of my application very subtle. Enter problem: When running my unit and component tests, I need to have the task run as it is supposed to be, for, like say 95 percent of my test cases. But of course I want to test the method itself. The subtle changes may arise after let's say half an hour or so. Not a good idea for a test suite. What I need is a way to speed things up for exactly this very test case.

Because #define isn't allowed to appear within code, the only way I see is to write an extra test case file for this test? I'd love something like this:

[TestMethod()]
public void Test_GetThisThingAndRunIt()
{
    // ...
    #if DEBUG
    #define CRV_TEST
    // run the test
    #undef CRV_TEST
    #endif
}

etc. and then, in the class to test, do something like

#if CRV_TEST
System.Threading.Thread.Sleep(1);
#else
System.Threading.Thread.Sleep(500);
#endif

How to? Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire