Skip to main content

Easily generate random unicode test data among other things

Project description

Generate Random Test Data.

These are just a bunch of functions designed to make it easier to test your code.

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

This is an overview of some of the methods found in the Testdata module, there are other methods (like get_birthday) that aren’t listed here, for the complete list just look at the source.

patch

Patching modules and classes

patch(mod, **patches)

Patches a module, instance, or class with the given patches.

Suppose you had a module like this:

# module foo.bar

def boom():
    return 1

class FooPatch(object):
    @classmethod
    def bam(cls): return boom()

Now you can easily patch it for testing:

def mock_boom():
    return 2

foo_bar = testdata.patch('foo.bar', boom=mock_boom)
print foo_bar.FooPatch.bam() # 2

# but you can also just pass in objects or modules

from foo.bar import FooPatch
FooPatch = testdata.patch(FooPatch, boom=mock_boom)
print FooPatch.bam() # 2

from foo import bar
bar = testdata.patch(bar, boom=mock_boom)
print bar.FooPatch.bam() # 2

Patching class instances

You can also patch a specific instance

Suppose you had a module like this:

# module foo.bar

class Foo(object):
    def boom(self): return 1

Now you can easily patch it for testing:

def mock_boom():
    return 2

foo = Foo()
foo_patched = testdata.patch(foo, boom=mock_boom)
print foo_patched.boom() # 2

# be aware though, the original instance was modified, foo_patched == foo
print foo.boom() # 2

create_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

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

create a file with contents

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

create_files

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. The file_dict is very similar to the create_modules param module_dict

file_dict = {
    "foo/bar.txt": "the foo file contents",
    "baz.txt": "the baz file contents",
}
f = testdata.create_files(file_dict)

get_file

get_file(path="", tmpdir="")

This will return a Filepath instance that you can manipulate but unlike create_file it won’t actually create the file, just give you a path to a file that could be created.


create_module

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

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

get_ascii(str_size=0)

return a string of ascii characters

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

get_md5

get_md5(val="")

return an md5 hash of val (if passed in) or a random val if val is empty

>>> testdata.get_md5()
'b165765400b30772f1d9b3975ce77320'

get_hash

get_hash(str_size=32)

return a random hash

>>> testdata.get_hash()
u"jYw3HseUl8GLoMc8QejLYFogC2lUYoUu"

get_bool

get_bool()

return a boolean (either True or False)

>>> testdata.get_bool()
False
>>> testdata.get_bool()
True

### get_float

python 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

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

return an integer between min_size and max_size.

>>> testdata.get_int()
3820706953806377295

get_name

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_email

get_email(name=u'')

returns a random email address in the ascii range.

>>> testdata.get_email()
u'shelley@gmail.com'

get_str

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

get_url()

return a random url.

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

get_words

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"

get_past_datetime

get_past_datetime([now])

return a datetime guaranteed to be in the past from now

>>> testdata.get_past_datetime()
datetime.datetime(2000, 4, 2, 13, 40, 11, 133351)

get_future_datetime

get_future_datetime([now])

return a datetime guaranteed to be in the future from now

>>> testdata.get_future_datetime()
datetime.datetime(2017, 8, 3, 15, 54, 58, 670249)

get_between_datetime

get_between_datetime(start[, stop])

return a datetime guaranteed to be in the future from start and in the past from stop

>>> start = datetime.datetime.utcnow() - datetime.timedelta(days=100)
>>> testdata.get_between_datetime(start)
datetime.datetime(2017, 8, 3, 15, 54, 58, 670249)

Testing

Testing in 2.7 on most systems:

$ python -m unittest testdata_test

Testing in 3.5 on MacOS:

$ python3.5 -m unittest testdata_test

Project details


Download files

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

Source Distribution

testdata-0.6.14.tar.gz (34.1 kB view details)

Uploaded Source

File details

Details for the file testdata-0.6.14.tar.gz.

File metadata

  • Download URL: testdata-0.6.14.tar.gz
  • Upload date:
  • Size: 34.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for testdata-0.6.14.tar.gz
Algorithm Hash digest
SHA256 c39a94ab7b059279f68cdc9664bf803be73aac482f8db85323c262217c7f4ef1
MD5 51e0efc97368a4cfd88a3f3d5cdcbb88
BLAKE2b-256 2f43c071ce322d13f134b1799d0fcc3a50bd55f570b343120be5e876af70f778

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page