Automated test case generation tool for Python modules.
Project description
logitest
A lightweight module for logging and testing. No fluff, just tools to get the job done efficiently.
Installation
pip install logitest
Running in terminal
logitest examples/example_module examples/main.py
Running in python
from logitest import create_test_cases
create_test_cases(module_dirpath = "examples/example_module", main_filepath="examples/main.py")
Sample code to generate test cases when you have custom I/O objects
If you have external objects (non in-built python objects) as input/output in your functions/methods. Here is how you need to configure the module before trying to get the test cases.
Add custom type handlers
from logitest import get_dtype, add_to_config
# Create custom load and dump functions
import fitz
def load_pdf(filepath):
doc = fitz.open(filepath)
return doc
dump_pdf = lambda doc, filepath: doc.save(filepath) # Flexible to lambda functions as well
# To know the datatype of the object, load a sample object and check like this:
doc = load_pdf("./COA requirements.pdf")
get_dtype(doc) # This will be used as the key for your new_type_handlers dictionary
> 'pymupdf.Document'
# Now, create a type handling dictionary as shown below
# Note: This new dictionary should have the name 'new_type_handlers'
new_type_handlers = {
"pymupdf.Document": {
"extension": ".pdf",
"load": load_pdf,
"dump": dump_pdf
}
}
# Now add the entire code in a triple string, like this:
type_handler_str = """import fitz
def load_pdf(filepath):
doc = fitz.open(filepath)
return doc
dump_pdf = lambda doc, filepath: doc.save(filepath)
new_type_handlers = {
"pymupdf.Document": {
"extension": ".pdf",
"load": load_pdf,
"dump": dump_pdf
}
}"""
Add custom Assertion mappings
# If you have any custom assert functions to use in pytest, follow this process:
# First create your custom assert function with required imports and global variables
import fitz # PyMuPDF
def assert_documents_equal(doc1_path, doc2_path, tolerance=0):
doc1 = fitz.open(doc1_path)
doc2 = fitz.open(doc2_path)
if len(doc1) != len(doc2):
raise AssertionError(f"Documents have different number of pages: {len(doc1)} != {len(doc2)}")
for page_num in range(len(doc1)):
pix1 = doc1[page_num].get_pixmap()
pix2 = doc2[page_num].get_pixmap()
if pix1.size != pix2.size or pix1.samples != pix2.samples:
diff = sum(
abs(a - b)
for a, b in zip(pix1.samples, pix2.samples)
)
if diff > tolerance:
raise AssertionError(
f"Page {page_num + 1} differs beyond tolerance level {tolerance}. "
f"Difference: {diff}."
)
return True
# Create a new assertion mapping dictionary
# Note, the name of this dictionary should new_assertion_mapping
new_assertion_mapping = {
"pymupdf.Document": ( # key is as usual the object it can test
"from logitest.config import assert_documents_equal", # standard import statement to import the custom function; as this functions goes to config
"assert_documents_equal" # custom function name
)
}
# Now add this entire code in triple quotes:
assertion_mapping_str = """import fitz # PyMuPDF
def assert_documents_equal(doc1_path, doc2_path, tolerance=0):
doc1 = fitz.open(doc1_path)
doc2 = fitz.open(doc2_path)
if len(doc1) != len(doc2):
raise AssertionError(f"Documents have different number of pages: {len(doc1)} != {len(doc2)}")
for page_num in range(len(doc1)):
pix1 = doc1[page_num].get_pixmap()
pix2 = doc2[page_num].get_pixmap()
if pix1.size != pix2.size or pix1.samples != pix2.samples:
diff = sum(
abs(a - b)
for a, b in zip(pix1.samples, pix2.samples)
)
if diff > tolerance:
raise AssertionError(
f"Page {page_num + 1} differs beyond tolerance level {tolerance}. "
f"Difference: {diff}."
)
return True
new_assertion_mapping = {"pymupdf.Document": ("from logitest.config import assert_documents_equal", "assert_documents_equal")}
"""
Adding custom code to config
# The final line for configuring the module
add_to_config(type_handling_str=type_handler_str, assertion_mapping_str=assertion_mapping_str)
Create and run tests
# Finally to create the test cases for your code
from logitest import create_test_cases
# Pass your module directory path and main.py which uses this code to run your module pipeline
create_test_cases(module_dirpath = "examples/example_module", main_filepath="examples/main.py")
Show your support
Be sure to drop a if you like the project!
Contributing
Contributions, issues and feature requests are always welcome!
Feel free to check the issues page.
Author
License
This project is licensed under the MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
logitest-0.1.3.tar.gz
(26.3 kB
view details)
Built Distribution
logitest-0.1.3-py3-none-any.whl
(28.4 kB
view details)
File details
Details for the file logitest-0.1.3.tar.gz
.
File metadata
- Download URL: logitest-0.1.3.tar.gz
- Upload date:
- Size: 26.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | edb95bf0eb506c281df5f2a4630002066c3b2630c4bd13f5a6d3c0fc5c664c49 |
|
MD5 | 36da3c21010139551feebfac8ae49425 |
|
BLAKE2b-256 | 79fade23b2183d8865a3a14e0476e71e858d42fbe2487c2df0aa09a35a8dbdca |
File details
Details for the file logitest-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: logitest-0.1.3-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf052b101c3b959424decb1a24ae62c2c787474da14a39e7d6e237558921521a |
|
MD5 | 939c46df5e05b109b05bb76fba42c69f |
|
BLAKE2b-256 | da420b90b3e98c2da1dfcf24ee4c88479ae9e7229c3fedab00c9c2ce3d329fff |