I agree with the comments saying to avoid doing this in general - but I've done this in the past myself. IIRC, it was used to switch between a "unit test database" (i.e. throwaway) and a "test database with useful data in" - which had been accidentally wiped by unit tests one too many times...
Our solution was to have a UnitTestDetector class (or something like that) which had a single static property, "InUnitTest". This would be detected by whether NUnit was loaded in the current appdomain (again, IIRC). After the first probing, the result would be cached to prevent it from being a performance hit.