Skip to content

Commit

Permalink
added test_mode() method
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelcourt authored and zupo committed Oct 9, 2013
1 parent a3c6447 commit 6130317
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Changes
parameter, then lookup will fail. In all other cases, there should be no
difference.

- Add api.env.debug_mode(), refs #125.
- Add api.env.debug_mode() and api.env.test_mode(), refs #125.
[sdelcourt]

- Move most of text from docs/index.rst to README.rst so its also visible on
Expand Down
15 changes: 15 additions & 0 deletions docs/env.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ To know if your zope instance is running in debug mode, use
if in_debug_mode:
print 'Zope is in debug mode'
Test mode
---------

To know if your plone instance is running in a test runner, use
:meth:`api.env.test_mode`.

.. code-block:: python
from plone import api
in_test_mode = api.env.test_mode()
if in_test_mode:
print 'You are in a test runner'
Further reading
---------------

Expand Down
17 changes: 17 additions & 0 deletions src/plone/api/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from zope.globalrequest import getRequest

import Globals
import traceback

IS_TEST = None


@at_least_one_of('username', 'user')
Expand Down Expand Up @@ -161,3 +164,17 @@ def getWrappedOwner(self):
def debug_mode():
"""Returns True if your zope instance is running in debug mode."""
return Globals.DevelopmentMode


def test_mode():
"""Returns True if you are running the zope test runner."""
from plone.api import env

if env.IS_TEST is None:
env.IS_TEST = False
for frame in traceback.extract_stack():
if 'zope/testing/testrunner' in frame[0]:
env.IS_TEST = True
break

return env.IS_TEST
5 changes: 5 additions & 0 deletions src/plone/api/tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,8 @@ def test_debug_mode(self):
self.assertEqual(debug_mode(), True)
Globals.DevelopmentMode = False
self.assertEqual(debug_mode(), False)

def test_test_mode(self):
"""Tests that test_mode() returns True as we are in a test runner."""
from plone.api.env import test_mode
self.assertEqual(test_mode(), True)

0 comments on commit 6130317

Please sign in to comment.