API Reference

testfixtures.test_date(year=2001, month=1, day=1, delta=None, delta_type='days', strict=False)

A function that returns a mock object that can be used in place of the datetime.date class but where the return value of today() can be controlled.

If a single positional argument of None is passed, then the queue of dates to be returned will be empty and you will need to call set() or add() before calling today().

If an instance of date is passed as a single positional argument, that will be used as the first date returned by today()

Parameters
  • year – An optional year used to create the first date returned by today().

  • month – An optional month used to create the first date returned by today().

  • day – An optional day used to create the first date returned by today().

  • delta – The size of the delta to use between values returned from today(). If not specified, it will increase by 1 with each call to today().

  • delta_type – The type of the delta to use between values returned from today(). This can be any keyword parameter accepted by the timedelta constructor.

  • strict – If True, calling the mock class and any of its methods will result in an instance of the mock being returned. If False, the default, an instance of date will be returned instead.

The mock returned will behave exactly as the datetime.date class with the exception of the following members:

tdate.add(*args, **kw)

This will add the datetime.date created from the supplied parameters to the queue of dates to be returned by today(). An instance of date may also be passed as a single positional argument.

tdate.set(*args, **kw)

This will set the datetime.date created from the supplied parameters as the next date to be returned by today(), regardless of any dates in the queue. An instance of date may also be passed as a single positional argument.

tdate.tick(*args, **kw)

This method should be called either with a timedelta as a positional argument, or with keyword parameters that will be used to construct a timedelta.

The timedelta will be used to advance the next date to be returned by today().

classmethod tdate.today()

This will return the next supplied or calculated date from the internal queue, rather than the actual current date.

testfixtures.test_datetime(year=2001, month=1, day=1, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, delta=None, delta_type='seconds', date_type=datetime.date, strict=False)

A function that returns a mock object that can be used in place of the datetime.datetime class but where the return value of now() can be controlled.

If a single positional argument of None is passed, then the queue of datetimes to be returned will be empty and you will need to call set() or add() before calling now() or utcnow().

If an instance of datetime is passed as a single positional argument, that will be used as the first date returned by now()

Parameters
  • year – An optional year used to create the first datetime returned by now().

  • month – An optional month used to create the first datetime returned by now().

  • day – An optional day used to create the first datetime returned by now().

  • hour – An optional hour used to create the first datetime returned by now().

  • minute – An optional minute used to create the first datetime returned by now().

  • second – An optional second used to create the first datetime returned by now().

  • microsecond – An optional microsecond used to create the first datetime returned by now().

  • tzinfo – An optional tzinfo that will be used to indicate the timezone intended for the values returned by returned by now(). It will be used to correctly calculate return values when tz is passed to now() and when utcnow() is called.

  • delta – The size of the delta to use between values returned from now(). If not specified, it will increase by 1 with each call to now().

  • delta_type – The type of the delta to use between values returned from now(). This can be any keyword parameter accepted by the timedelta constructor.

  • date_type – The type to use for the return value of the date() method. This can help with gotchas that occur when type checking if performed on values returned by the mock’s date() method.

  • strict – If True, calling the mock class and any of its methods will result in an instance of the mock being returned. If False, the default, an instance of datetime will be returned instead.

The mock returned will behave exactly as the datetime.datetime class with the exception of the following members:

tdatetime.add(*args, **kw)

This will add the datetime.datetime created from the supplied parameters to the queue of datetimes to be returned by now() or utcnow(). An instance of datetime may also be passed as a single positional argument.

tdatetime.set(*args, *kw)

This will set the datetime.datetime created from the supplied parameters as the next datetime to be returned by now() or utcnow(), clearing out any datetimes in the queue. An instance of datetime may also be passed as a single positional argument.

tdatetime.tick(*args, **kw)

This method should be called either with a timedelta as a positional argument, or with keyword parameters that will be used to construct a timedelta.

The timedelta will be used to advance the next datetime to be returned by now() or utcnow().

classmethod tdatetime.now([tz])
Parameters

tz – An optional timezone to apply to the returned time. If supplied, it must be an instance of a tzinfo subclass.

This will return the next supplied or calculated datetime from the internal queue, rather than the actual current datetime.

If tz is supplied, it will be applied to the datetime that would have have been returned from the internal queue, treating that datetime as if it were in the UTC timezone.

classmethod tdatetime.utcnow()

This will return the next supplied or calculated datetime from the internal queue, rather than the actual current UTC datetime.

No timezone will be applied, even that supplied to the constructor.

classmethod tdatetime.date()

This will return the date component of the current mock instance, but using the date type supplied when the mock class was created.

testfixtures.test_time(year=2001, month=1, day=1, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, delta=None, delta_type='seconds')

A function that returns a mock object that can be used in place of the time.time function but where the return value can be controlled.

If a single positional argument of None is passed, then the queue of times to be returned will be empty and you will need to call set() or add() before calling the mock.

If an instance of datetime is passed as a single positional argument, that will be used to create the first time returned.

Parameters
  • year – An optional year used to create the first time returned.

  • month – An optional month used to create the first time.

  • day – An optional day used to create the first time.

  • hour – An optional hour used to create the first time.

  • minute – An optional minute used to create the first time.

  • second – An optional second used to create the first time.

  • microsecond – An optional microsecond used to create the first time.

  • delta – The size of the delta to use between values returned. If not specified, it will increase by 1 with each call to the mock.

  • delta_type – The type of the delta to use between values returned. This can be any keyword parameter accepted by the timedelta constructor.

The mock additionally has the following methods available on it:

ttime.add(*args, **kw)

This will add the time specified by the supplied parameters to the queue of times to be returned by calls to the mock. The parameters are the same as the datetime.datetime constructor. An instance of datetime may also be passed as a single positional argument.

ttime.set(*args, **kw)

This will set the time specified by the supplied parameters as the next time to be returned by a call to the mock, regardless of any times in the queue. The parameters are the same as the datetime.datetime constructor. An instance of datetime may also be passed as a single positional argument.

ttime.tick(*args, **kw)

This method should be called either with a timedelta as a positional argument, or with keyword parameters that will be used to construct a timedelta.

The timedelta will be used to advance the next time to be returned by a call to the mock.

testfixtures.not_there

A singleton used to represent the absence of a particular attribute.