Skip to main content

pytest assert writer

Project description

# asserted
[![Travis Status](](
[![GitHub Releases](](
[![PyPI version](](

asserted is a simple tool that writes a pytest for the object your pass to it.
This is no way near perfect, you still have to handle imports, fixtures at but its handy if you need to check that all the properties are correct.

#### CLI:
asserted path/to/file
usage: asserted [-h] [-oa] [-wf] [-tp] [-sp SAVE_PATH] [-sm] [-st] [-q] fp

positional arguments:
fp The file you want to write test for.

optional arguments:
-h, --help show this help message and exit
-oa, --only_attributes
only attributes
-wf, --write_full_tests
write tests
-tp, --test_prefix test prefix
-sp SAVE_PATH, --save_path SAVE_PATH
-sm, --separate_methods
Separate methods to there own test functions.
-st, --sort_iterables
Separate methods to there own test functions.
-q Disable logging.

#### Code:
Say you have a class like:
class Ex(object):
def __init__(self): = 'data'
self.att1 = 'att1'
self.is_list = []
self.is_dict = {}
self.is_int = 1
self.is_float = 0.5
self.is_generator_expression = (i for i in range(2))
self.is_datetime = datetime.datetime(1970, 1, 1)

def props(self):
return 'props'

def method(self):
return 'method'

async def async_metod(self):
return 'async_metod'

async def async_metod_two(self):
return 'async_metod_two'

def a_classmethod(cls):
return 'a_classmethod'

def a_staticmethod():
return 'a_staticmethod'

def a_coro_with_return(self):
return 'a_coro_with_return'

def a_generator_function(self):
for i in range(3):
yield i

def a_missing_arg(self, arg):
return arg

assert_write(asserted.example_class.Ex(), write_full_tests=True, separate_methods=True, sort_iterables=True)
# Will create the code below.

def test_ex():
ex = asserted.example_class.Ex()
assert ex.att1 == "att1"
assert == "data"
assert str( == "1970-01-01"
assert sorted(ex.is_dict.items()) == [('a', 'a'), ('b', 'b')]
assert ex.is_false is False
assert ex.is_float == 0.5
assert sorted(list(ex.is_generator_expression)) == [0, 1]
assert ex.is_int == 1
assert sorted(ex.is_list) == [1, 2, 3, 4, 5]
assert ex.is_true is True
assert sorted(ex.is_tuple) == [1, 2, 3]
assert ex.props == "props"

def test_ex_classmethod():
ex = asserted.example_class.Ex()
assert ex.a_classmethod() == "a_classmethod"

def test_ex_coro_with_return():
ex = asserted.example_class.Ex()

async def gogo():
a_coro_with_return = await ex.a_coro_with_return()
assert a_coro_with_return == "a_coro_with_return"

def test_ex_generator_function():
ex = asserted.example_class.Ex()
assert sorted(list(ex.a_generator_function())) == [0, 1, 2]

def test_ex_a_staticmethod():
ex = asserted.example_class.Ex()
assert ex.a_staticmethod() == "a_staticmethod"

def test_ex_async_metod():
ex = asserted.example_class.Ex()

async def gogo():
async_metod = await ex.async_metod()
assert async_metod == "async_metod"

def test_ex_async_metod_two():
ex = asserted.example_class.Ex()

async def gogo():
async_metod_two = await ex.async_metod_two()
assert async_metod_two == "async_metod_two"

def test_ex_method():
ex = asserted.example_class.Ex()
assert ex.method() == "method"

def test_ex_missing_a_arg():
ex = asserted.example_class.Ex()
assert ex.missing_a_arg() == "Missing_ARG"


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

asserted-0.0.7.tar.gz (11.4 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page