django_test_tools package¶
Subpackages¶
Submodules¶
django_test_tools.app_manager module¶
django_test_tools.apps module¶
django_test_tools.assert_utils module¶
-
class
django_test_tools.assert_utils.AssertionWriter(**kwargs)[source]¶ Bases:
objectThis class generates assertions using Django practice of putting actual value first and then expected value.
-
django_test_tools.assert_utils.write_assert_list(filename, dictionary_list, variable_name)[source]¶ - Function to generate assertions for a dictionary or list content. :param filename: :param dictionary_list: :param variable_name: :return:
Note
Deprecated: Use assert_utils.write_assertions instead
-
django_test_tools.assert_utils.write_assertions(dictionary_list, variable_name, **kwargs)[source]¶ Writes assertions using Django practice of putting actual value first and then expected value to a file. If no filename is supplied it will generate a file in the settings.TEST_OUTPUT_PATH folder with the variable_name and the current date. By default key named created and modified will be excluded.
Parameters: - dictionary_list – <list> or <dict> dictionary or list of values
- variable_name – <str> name of the variable
- kwargs – filename <str>String. Full path to the output file.
- kwargs – excluded_keys <list>list of strings. List with keys to exclude
- kwargs – type_only <boolean> Check only for types instead of values. Default false
Returns: filename string.
django_test_tools.excel module¶
-
class
django_test_tools.excel.ExcelAdapter[source]¶ Bases:
object-
classmethod
convert_to_dict(filename, sheet_name=None)[source]¶ Reads an Excel file and converts every row into a dictionary. All values are converted to strings. Assumes first row contains the name of the attributes.
Parameters: - filename – <str> Excel filename
- sheet_name – <str> Name of the sheet
Returns: <list> A list of dictionaries.
-
classmethod
convert_to_list(filename, sheet_name=None, has_header=True)[source]¶ Reads an Excel file and converts every row into a dictionary. All values are converted to strings. Assumes first row contains the name of the attributes.
Parameters: - filename – <str> Excel filename
- sheet_name – <str> Name of the sheet
Returns: <list> A list of dictionaries.
-
classmethod
django_test_tools.file_utils module¶
-
class
django_test_tools.file_utils.TemporaryFolder(base_name, delete_on_exit=True)[source]¶ Bases:
object
-
django_test_tools.file_utils.add_date(filename, **kwargs)[source]¶ Adds to a filename the current date and time in ‘%Y%m%d_%H%M’ format. For a filename /my/path/myexcel.xlsx the function would return /my/path/myexcel_20170101_1305.xlsx. If the file already exists the function will add seconds to the date to attempt to get a unique name.
The function will detect if another file exists with the same name if it exist it will append seconds to the filename. For example if file /my/path/myexcel_20170101_1305.xlsx alread exist thte function will return /my/path/myexcel_20170101_130521.xlsx.
Parameters: - filename – string with fullpath to file or just the filename
- kwargs – dictionary. date_position: suffix or preffix, extension: string to replace extension
Returns: string with full path string including the date and time
-
django_test_tools.file_utils.create_dated(filename)[source]¶ Based on the filename will create a full path filename including the date and time in ‘%Y%m%d_%H%M’ format. The path to the filename will be set in the TEST_OUTPUT_PATH settings variable.
If the TEST_OUTPUT_PATH folder doesn’t exist the function will create it.
Parameters: filename – base filename. my_excel_data.xlsx for example Returns: string, full path to file with date and time in the TEST_OUTPUT_PATH folder
-
django_test_tools.file_utils.hash_file(filename, algorithm='sha1', block_size=65536)[source]¶ Creates a unique hash for a file.
Parameters: - filename – String with the full path to the file
- algorithm – String Algorithm to create the hash
- block_size – int for the size of the block while reading the file
Returns: string the hash for the file
-
django_test_tools.file_utils.json_serial(obj)[source]¶ JSON serializer for objects not serializable by default json code taken from: https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable
-
django_test_tools.file_utils.parametrized(dec)[source]¶ Need to study this code. Got it from http://stackoverflow.com/questions/5929107/python-decorators-with-parameters
Parameters: dec – Returns:
-
django_test_tools.file_utils.serialize_data(data, output_file=None, format='json', **kwargs)[source]¶ Quick function to serialize a data to file. The data keys will be saved in an alphabetical order for consistency purposes. If no output_file is supplied the function will created a dated file in the settings.TEST_OUTPUT_PATH folder. if the output_file is a folder the dated file will be created on the supplied folder with the serialized date. if the output_file is a file the data will be serialized to thar file
Parameters: - data – Dictionary or list to serialize
- format – Format to serialize to. Currently json is the only one supported
- output_file – File to output the data to
- kwargs –
-
django_test_tools.file_utils.shorten_path(path, level=2, current_level=1)[source]¶ This method shortens the path by eliminating the folders on top.
filename = '/user/documents/personal/file.txt' shortened = shorten_path(filename) self.assertEqual(shortened, 'personal/file.txt')
Parameters: - path – string full path for the filename
- level – int, number of levels to show.
- current_level – int, recursing level.
Returns: string shortened path
django_test_tools.mixins module¶
-
class
django_test_tools.mixins.TestCommandMixin[source]¶ Bases:
object- This mixin helps capture the output of a command written with the stdout.write() method and
- the stderr.write
class TestYourCommand(TestCommandMixin, TestCase): def test_your_command_action(self): call_command('your_command', 'your_argument', stdout=self.content, stderr=self.error_content) results = self.get_results() self.assertEqual(23, len(results))
-
class
django_test_tools.mixins.TestFixtureMixin(app_name=None, **kwargs)[source]¶ Bases:
objectThis a mixin to add to test cases to easily access fixtures. It assumes the the you have a package for your tests named tests and your fixtures are in a folder named fixtures within your tests package and that you have settings variable named APPS_DIR pointing tou your applications folder (this is created by Cookiecutter by default). Your tests package should look like this.
├── clinics│ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── exceptions.py │ ├── forms.py │ ├── migrations │ ├── models.py │ ├── tests │ │ ├── __init__.py │ │ ├── factories.py │ │ ├── fixtures │ │ │ ├── data.json │ │ │ ├── model_data.txt │ │ ├── test_forms.py │ │ ├── test_models.py │ ├── urls.py │ └── views.py
For the above exmple
class TestClinicAdapter(TestFixtureMixin, TestCase): def setUp(self) -> None: self.app_name = 'clinics' def test_parse(self): clinics_dictionary = self.get_fixture_json('data.json') ... def test_read(self): filename = self.get_fixture_fullpath('model.txt') ...
-
app_name= None¶
-
-
class
django_test_tools.mixins.TestOutputMixin[source]¶ Bases:
object-
clean_output= True¶
-
get_excel_content(filename, sheet_name=None)[source]¶ Reads the content of an excel file and returns the content a as list of row lists. :param filename: string full path to the filename :param sheet_name: string. Name of the sheet to read if None will read the active sheet :return: a list containing a list of values for every row.
-
django_test_tools.models module¶
django_test_tools.urls module¶
django_test_tools.utils module¶
-
class
django_test_tools.utils.Timer[source]¶ Bases:
objectClass to measure time elapsed
Example:
def test_performance(self): with Timer() as stopwatch: web_service = WebServiceUtil() web_service.consume_date(12) elapsed_milliseconds = stopwatch.elapsed*1000 logger.debug('Elapsed: {} ms'.format(elapsed_milliseconds)) self.assertTrue(elapsed_milliseconds <= 500)
-
running¶
-
-
django_test_tools.utils.add_date_to_filename(filename, **kwargs)[source]¶ Adds to a filename the current date and time in ‘%Y%m%d_%H%M’ format. For a filename /my/path/myexcel.xlsx the function would return /my/path/myexcel_20170101_1305.xlsx. If the file already exists the function will add seconds to the date to attempt to get a unique name.
param filename: string with fullpath to file or just the filename param kwargs: dictionary. date_position: suffix or preffix, extension: string to replace extension return: string with full path string incluiding the date and time Note
Deprecated: Should use django_test_tools.file_utils.add_date() function
-
class
django_test_tools.utils.cd(newPath)[source]¶ Bases:
objectContext manager for changing the current working directory
-
django_test_tools.utils.convert_to_snake_case(camel_case)[source]¶ Converts a CamelCase name to snake case. ..code-block:: python
camel_case = ‘OperatingSystemLongName’ snake_case = convert_to_snake_case(camel_case) self.assertEqual(snake_case, ‘operating_system_long_name’)Parameters: camel_case – string. Camel case name Returns: string. Snake case name
-
django_test_tools.utils.create_output_filename_with_date(filename)[source]¶ Based on the filename will create a full path filename incluidn the date and time in ‘%Y%m%d_%H%M’ format. The path to the filename will be set in the TEST_OUTPUT_PATH settings variable.
param filename: base filename. my_excel_data.xlsx for example return: string, full path to file with date and time in the TEST_OUTPUT_PATH folder Note
Deprecated: Should use django_test_tools.file_utils.create_dated() function
-
django_test_tools.utils.daterange(start_date, end_date)[source]¶ DEPRECATED use utils.weekdays() function instead :param start_date: :param end_date: :return:
-
django_test_tools.utils.datetime_to_local_time(date_time)[source]¶ Converts a naive date to a time zoned date based in hte setting.TIME_ZONE variable. If the date has already a time zone it will localize the date. :param date_time: <date> or <datetime> to be localized :return: localized non naive datetime
-
django_test_tools.utils.deprecated(func)[source]¶ - This is a decorator which can be used to mark functions
- as deprecated. It will result in a warning being emitted when the function is used.
from: https://wiki.python.org/moin/PythonDecoratorLibrary#CA-92953dfd597a5cffc650d5a379452bb3b022cdd0_7
-
django_test_tools.utils.weekdays(start_date, end_date)[source]¶ Returns a generator with the dates of the week days between the start and end date
start_date = datetime.date(2016, 10, 3) # Monday end_date = datetime.date(2016, 10, 7) # Friday days = list(weekdays(start_date, end_date)) self.assertEqual(5, len(days))
Parameters: - start_date – date. Start date
- end_date – date. End date