Unit testing C#/.NET classes which make use of static variables (unit test process segregation) -
Unit testing C#/.NET classes which make use of static variables (unit test process segregation) -
i've got codebase makes utilize of static variables in number of cases makes sense, illustration flagging something's run 1 time since launch, etc etc.
of course, can lead issues unit testing whereupon order matters , outcome of test on method of such class may depend on whether other code has been nail before, etc. understand of testtools.unittesting whenever run set of unit tests, within same project run within same process, static state maintained test test, whereas unit test project boundary implies process boundary , thus, if run 3 tests project 4th project b, state maintained 1>2>3 (in whatever order run) 4 virgin , static state default.
so questions two: 1) assessment right unit test projects have 1:1 relationship processes when tests run in grouping (run or run selected), or there more nuance there i'm missing? 2) regardless, if have test needs fresh, default static state custom objects uses , tests, have more elegant alternative creating giving own test project?
statics not per process, per application domain, represented appdomain class. single process can have several appdomains. appdomains have own statics, can provide sandboxing partially trusted code, , can unloaded allowing newer versions of same assembly hot swapped without restarting application.
your test runner creating new appdomain per test assembly each assembly gets own static variables. can create appdomain same on fly. not typically great pure unit tests, i've had work "rude" libraries kinds of static initialization , caching cannot cleaned out or reset. in sorts of integration scenarios useful.
you can utilize helper run simple delegate:
public static class appdomainhelper { public static void run(action action) { var domain = appdomain.createdomain("test domain"); seek { domain.docallback(new crossappdomaindelegate(action)); } { appdomain.unload(domain); } } }
one caution delegate action
passed run
cannot have captured variables (as in lambda). doesn't work because compiler generate hidden class not serializable , cannot pass through appdomain boundary.
c# .net unit-testing
Comments
Post a Comment