pytest tests.random_data

class tests.random_data.RandomData(seed: Optional[int] = None)

Bases: object

This class provides a variety of randomized data, maintaining the seed as a property so that, if necessary it can be queried and used to provide the same sequence of data in a later test run.

get_current_state() tuple[Any, ...]

Retrieves the current state of the random number generator

Returns

the current state of the random number generator

Return type

tuple[Any, …]

get_init_state()

Retrieves the randon.getstate() value that was saved when the RandomData instance was created

Returns

the initial state of the random number generator

Return type

tuple[Any]

static random_alpha_string(length: int, initial_caps: bool = True, no_spaces: bool = False) str

Returns a string of random alpha characters

Parameters
  • length (int) – the length of the string to be returned

  • initial_caps (bool) – should the initial alpha char in the string be capitalized

  • no_spaces (bool) – are spaces allowed in the returned string

Returns

the produced string

Return type

str

static random_bool() bool

Returns a random boolean value

Returns

produced bool value

Return type

bool

static random_date(lower: date, upper: date) date

Returns a random datetime.date within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (datetime.date) – lower boundary

  • upper (datetime.date) – upper boundary

Returns

random date value

Return type

datetime.date

static random_date_pair(lower: date, upper: date)

Returns a sorted pair random datetime.date within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (datetime.date) – lower boundary

  • upper (datetime.date) – upper boundary

Returns

random date value

Return type

tuple[datetime.date, datetime.date]

static random_datetime(lower: datetime, upper: datetime) datetime

Returns a random datetime.datetime within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (datetime.datetime) – lower boundary

  • upper (datetime.datetime) – upper boundary

Returns

random datetime value

Return type

datetime.datetime

static random_datetime_pair(lower: datetime, upper: datetime) tuple[datetime.datetime, datetime.datetime]

Returns a sorted pair random datetime.datetime within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (datetime.datetime) – lower boundary

  • upper (datetime.datetime) – upper boundary

Returns

random time value

Return type

tuple[datetime.datetime, datetime.datetime]

static random_dec(lower: [<class 'float'>, <class 'decimal.Decimal'>], upper: [<class 'float'>, <class 'decimal.Decimal'>], precision: int = 2)

Returns a random Decimal within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (float or decimal.Decimal) – lower boundary

  • upper (float or decimal.Decimal) – upper boundary

Returns

random Decimal value

Return type

decimal.Decimal

static random_dec_pair(self, lower: [<class 'float'>, <class 'decimal.Decimal'>], upper: [<class 'float'>, <class 'decimal.Decimal'>], precision: int = 2)

Returns a sorted tuple of random Decimal values within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (float or decimal.Decimal) – lower boundary

  • upper (float or decimal.Decimal) – upper boundary

Returns

a pair of sorted int values

Return type

tuple[decimal.Decimal]

static random_dict_item(dictx: dict) tuple[Any, Any]

Returns a randomly chosen key/value pair from the provided dict

Parameters

dictx (dict) – the dict from which a key/value pair will be selected

Returns

a key/value pair

Return type

tuple[Any, Any]

static random_int(lower: int, upper: int)

Returns a random integer within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (int) – lower boundary

  • upper (int) – upper boundary

Returns

random int value

Return type

int

static random_int_pair(lower: int, upper: int) tuple[int, int]

Returns a sorted tuple of random int values within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (int) – lower boundary

  • upper (int) – upper boundary

Returns

a pair of sorted int values

Return type

tuple[int]

static random_time(lower: time, upper: time)

Returns a random datetime.time within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (datetime.time) – lower boundary

  • upper (datetime.time) – upper boundary

Returns

random time value

Return type

datetime.time

static random_time_pair(lower: time, upper: time) tuple[datetime.time, datetime.time]

Returns a sorted pair random datetime.time within a range expressed by inclusive upper and lower bounds

Parameters
  • lower (datetime.time) – lower boundary

  • upper (datetime.time) – upper boundary

Returns

random time value

Return type

tuple[datetime.time, datetime.time]

set_state(state: tuple[Any, ...], set_as_initial: bool)

Set the random number generator state to a particular value

Parameters
  • state (tuple[Any, ...]) – state to be established

  • set_as_initial (bool) – should this state be saved in the initial state property

Returns

None