diff --git a/product/ERP5/tests/testOrder.py b/product/ERP5/tests/testOrder.py
index e52526919cfcf3dc818995d3c85facd7345d1f63..eb8c093973784fda2a4d4b62f27890bc74e87f2c 100644
--- a/product/ERP5/tests/testOrder.py
+++ b/product/ERP5/tests/testOrder.py
@@ -51,7 +51,8 @@ class TestOrderMixin(SubcontentReindexingWrapper):
   order_cell_portal_type = 'Sale Order Cell'
   applied_rule_portal_type = 'Applied Rule'
   simulation_movement_portal_type = 'Simulation Movement'
-  datetime = DateTime()
+  # see comment about self.datetime on afterSetUp() below
+  datetime = DateTime() - 2
   packing_list_portal_type = 'Sale Packing List'
   packing_list_line_portal_type = 'Sale Packing List Line'
   packing_list_cell_portal_type = 'Sale Packing List Cell'
@@ -94,15 +95,28 @@ class TestOrderMixin(SubcontentReindexingWrapper):
       preference.enable()
     self.tic()
 
-  def afterSetUp(self, quiet=1, run=1):
+  def afterSetUp(self):
+    # XXX-Leo: cannot call super here, because other classes call
+    # SuperClass.afterSetUp(self) directly... this needs to be cleaned
+    # up, including consolidating all conflicting definitions of
+    # .createCategories()
+    #super(TestOrderMixin, self).afterSetUp()
     self.login()
-    portal = self.getPortal()
     self.category_tool = self.getCategoryTool()
     portal_catalog = self.getCatalogTool()
     #portal_catalog.manage_catalogClear()
     self.createCategories()
     self.validateRules()
     self.setUpPreferences()
+    # pin datetime on the day before yesterday, to make sure that:
+    #
+    # 1. All calculations are done relative to the same time
+    # 2. We don't get random failures when tests run close to midnight
+    self.pinDateTime(self.datetime)
+
+  def beforeTearDown(self):
+    self.unpinDateTime()
+    super(TestOrderMixin, self).beforeTearDown()
 
   def createCurrency(self):
     currency_module = self.getPortal().currency_module
diff --git a/product/ERP5Type/tests/ERP5TypeTestCase.py b/product/ERP5Type/tests/ERP5TypeTestCase.py
index cafbfef7dc27c84832876c46ea8fa0ee24a2f457..8a391c7096a04cba17aa904f9d5a79713222e15a 100644
--- a/product/ERP5Type/tests/ERP5TypeTestCase.py
+++ b/product/ERP5Type/tests/ERP5TypeTestCase.py
@@ -26,6 +26,7 @@ from hashlib import md5
 from warnings import warn
 from ExtensionClass import pmc_init_of
 from ZTUtils import make_query
+from DateTime import DateTime
 
 # XXX make sure that get_request works.
 import Products.ERP5Type.Utils
@@ -275,6 +276,21 @@ def profile_if_environ(environment_var_name):
       # No profiling, return identity decorator
       return lambda self, method: method
 
+# Patch DateTime to allow pinning the notion of "now".
+assert getattr(DateTime, '_original_parse_args', None) is None
+DateTime._original_parse_args = DateTime._parse_args
+
+_pinned_date_time = None
+
+def _parse_args(self, *args, **kw):
+  if _pinned_date_time is not None and (not args or args[0] == None):
+    # simulate fixed "Now"
+    args = (_pinned_date_time,) + args[1:]
+  return self._original_parse_args(*args, **kw)
+
+_parse_args._original = DateTime._original_parse_args
+DateTime._parse_args = _parse_args
+
 class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase):
     """Mixin class for ERP5 based tests.
     """
@@ -354,6 +370,17 @@ class ERP5TypeTestCaseMixin(ProcessingNodeTestCase, PortalTestCase):
       cls.__bases__ = cls.__bases__[1:]
       pmc_init_of(cls)
 
+    def pinDateTime(self, date_time):
+      # pretend time has stopped at a certain date (i.e. the test runs
+      # infinitely fast), to avoid errors on tests that are started
+      # just before midnight.
+      global _pinned_date_time
+      assert date_time is None or isinstance(date_time, DateTime)
+      _pinned_date_time = date_time
+
+    def unpinDateTime(self):
+      self.pinDateTime(None)
+
     def getDefaultSitePreferenceId(self):
       """Default id, usefull method to override
       """