2

Does anyone know the least hacky way of determining if Python code is being run by a unit test?

Thanks!

4
  • Out of curiosity, why does it matter? If your function behaves differently when you're unit-testing it, then I imagine it's not a very good unit test ;-).
    – mgilson
    Dec 17, 2013 at 3:38
  • That is going to be extremely hacky by the mere premise of it. Why do you want to do this? Dec 17, 2013 at 3:38
  • 2
    If you want to mock out some components when unit testing, dependency injection is the way to go. Don't detect the test and do something differently. Dec 17, 2013 at 3:39
  • Guys, I'm aware this is very unusual and almost always a no-no. However, I'm in some very unusual circumstances where what I want to test is 'un-mockable', not without a huge amount of work, at least. The options are to have an imperfect test for the next several months or no tests for the next several months.
    – piyo
    Dec 17, 2013 at 4:21

1 Answer 1

1

I agree with all the comments.

Don't do this.

Your function/class/component should NOT behave differently under testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.