Skip to main content

Easy random test data generation

Project description

Generate Random Test Data

These are a bunch of functions I’ve been using for years over numerous projects. I’m finally bundling them up into a proper module instead of the copy/pasting I’ve been doing up until now.

To use testdata in your tests, just include the testdata.py module:

import testdata

To install, use Pip:

pip install testdata

Or, with Pip using Github:

pip install git+https://github.com/Jaymon/testdata#egg=testdata

Functions

create_file_structure(file_structure, tmpdir=u'')

This just makes it easy to create a lot of folders/files all at once.

base_dir = "/tmp"
tmpdir, created_dirs, created_files = testdata.create_file_structure(
  """
  /foo/
    /bar/
      /che.txt
      /bam.txt
    /baz
      /flam.txt
  """,
  tmpdir=base_dir
)

create_dir(path, tmpdir=u"")

create a directory hierarchy

base_dir = "/tmp"
d = testdata.create_dir("/foo/bar", base_dir)
print d # /tmp/foo/bar

create_file(path, contents=u"", tmpdir=u"")

create a file with contents

base_dir = "/tmp"
f = testdata.create_dir("/foo/bar.txt", "The file contents", base_dir)
print f # /tmp/foo/bar.txt

create_files(file_dict, tmpdir=u"")

Create a whole bunch of files, the file_dict key is the filename, the value is the contents of the file.

create_module(module_name, contents=u"", tmpdir=u"", make_importable=True)

create a module with python contents that can be imported

base_dir = "/tmp"
f = testdata.create_module("foo.bar", "class Che(object): pass", base_dir)
print f # /tmp/foo/bar.py

create_modules(module_dict, tmpdir=u"", make_importable=True)

create a whole bunch of modules at once

f = testdata.create_modules(
  {
    "foo.bar": "class Che(object): pass",
    "foo.bar.baz": "class Boom(object): pass",
    "foo.che": "class Bam(object): pass",
  }
)

get_ascii(str_size=0)

return a string of ascii characters

>>> testdata.get_ascii()
u'IFUKzVAauqgyRY6OV'

get_float(min_size=None, max_size=None)

return a floating point number between min_size and max_size.

>>> testdata.get_float()
2.932229899095845e+307

get_int(min_size=1, max_size=sys.maxsize)

return an integer between min_size and max_size.

>>> testdata.get_int()
3820706953806377295

get_name(name_count=2, as_str=True)

returns a random name that can be outside the ascii range (eg, name can be unicode)

>>> testdata.get_name()
u'jamel clarke-cabrera'

get_str(str_size=0, chars=None)

return random characters, which can be unicode.

>>> testdata.get_str()
u'q\x0bwZ\u79755\ud077\u027aYm\ud0d8JK\x07\U0010df418tx\x16'

get_url()

return a random url.

>>> testdata.get_url()
u'https://sK6rxrCa626TkQddTyf.com'

get_words(word_count=0, as_str=True)

return a random amount of words, which can be unicode.

>>> testdata.get_words()
u'\u043f\u043e\u043d\u044f\u0442\u044c \u043c\u043e\u0436\u043d\u043e felis, habitasse ultrices Nam \u0436\u0435\u043d\u0430'

Project details


Release history Release notifications

History Node

0.6.29

History Node

0.6.28

History Node

0.6.26

History Node

0.6.25

History Node

0.6.24

History Node

0.6.23

History Node

0.6.22

History Node

0.6.21

History Node

0.6.20

History Node

0.6.16

History Node

0.6.15

History Node

0.6.14

History Node

0.6.13

History Node

0.6.12

History Node

0.6.10

History Node

0.6.9

History Node

0.6.8

History Node

0.6.7

History Node

0.6.6

History Node

0.6.4

History Node

0.6.3

History Node

0.6.2

History Node

0.6.1

History Node

0.6.0

History Node

0.5.9

This version
History Node

0.5.8

History Node

0.5.3

History Node

0.4

History Node

0.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
testdata-0.5.8.tar.gz (27.8 kB) Copy SHA256 hash SHA256 Source None Jan 14, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page