質問

I'm using django-nose to run my tests and I need to know when they are running from inside my views/models methods/etc so I can skip stuff like checking cache values or calling some project-external resources for example only when running the tests.

It would be good to have some sort of environment variable available or something similar so I could do something like:

if os.environ.get('TESTS_ARE_RUNNING'):
    # skip this

or

if not TESTS_ARE_RUNNING:
  # do something

Does anybody know a way to accomplish this?

Thanks in advance for any help :)

役に立ちましたか?

解決

It is possible and you can see Detect django testing mode on how but you should not be doing this for this use case.

... so I can skip stuff like checking cache values or calling some project-external resources for example...

If you do this how do you intend to test that your application handles when these external resources are down or misbehaving? I would recommend that you look into the mock library and mock these external calls. That would also allow you to assert that these calls are properly executed and that you application can handle when they do not return as expected.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top