Skip to main content

Logging Test Case With Helper Methods

Project description

kissut - Python Unittesting Utilities

Provides a simple automatic test case that sets up a new log each time tests are run. Also provides some basic methods to assist with configuration and file management when running tests

Usage

Installation:

    pip install kissut

LoggingTestCase subclasses unittest.TestCase, so when creating your tests, simply subclass LoggingTestCase instead of TestCase. LoggingTestCase sets up a unittest directory wkdir/unittest/ and saves log directories there:

from kissut import LoggingTestCase
import os

# all test classes must derive from unittest.TestCase, which LoggingTestCase does...
class TestExample(LoggingTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        # this loads the test configuration file
        cls.cfg = cls.getConfig()

    def test_a_isTrue(self):
        s = self.randString()
        self.logger.debug("s: {}".format(s))
        self.assertTrue(s)

    def test_b_saveWorkData(self):
        self.cfg["data"] = "testData.json"
        fqfn = self.dumpWkDirData(data={"a": "b"}, filename=self.cfg["data"])
        self.logger.debug("file name: {}".format(self.cfg["data"]))
        self.assertTrue(os.path.exists(fqfn))

    def test_c_isPresent(self):
        self.logger.debug("cfg: {}".format(self.cfg))
        d = self.loadWkDirData(self.cfg["data"])
        self.logger.debug("data: {}".format(d))
        self.assertIsNotNone(d)

This produces the following directory structure after running it twice (the first time will abort as it creates the config file):

<project folder>
└── wkdir
    └── unittest
        ├── 20260306T155251
        │   └── log.txt
        ├── 20260306T160132
        │   ├── log.txt
        │   └── testData.json
        └── testConfig.json

Obviously loading a config file in the setUpClass method is not required, it is just something I find useful, especially when a connection string or url is required for testing.

Some additional things to note, a simple random string method is included, as are dump and save methods for data to the working directory, and an initialized logger on the test case object.

If you prefer to save the logs or test config in another location, create a session-specific or user environment variable that defines path to the alternative configuration file. The contents of the alternative configuration file should be formatted like the following, obviously replacing the values as appropriate:

{
    "logCfgFile": "wkdir/alt_unittest/logConfig.json",
    "testCfgFile": "wkdir/alt_unittest/testConfig.json"
}

Cloning For Development

Set up a virtual environment. Once an environment is set up, activate it and add dependencies with the following:

    pip install -r requirements/dev.txt

The dev.txt file includes:

  • BLACK, a code formatter, see notes at the bottom of this file for details
  • build, which provides the support for the building of the package

To run tests:

    python -m unittest discover -s tests/

The run the following to confirm the alternative configuration works, ensuring the unittest configuration and log directories get saved in the wkdir/alt_unittest directory:

    source tests/runAltUnittest.sh

Does the same thing as

    export KISSUT_CFG_FILE="tests/altConfig.json"
    python -m unittest discover -s tests/

Code Formatting

Code formatting is done using BLACK. BLACK allows almost no customization to how code is formatted with the exception of line length, which has been set to 119 characters.

Use the following to bulk format files:

    black . -l 144

Creating A New Release

Please do the following when making a new release, most are documented above:

  1. Run tests
  2. Code format
  3. Be sure to update the change log and _metadata.json with version and notes
  4. git add, commit, and push changes
  5. run the following code to generate a wheel:
    python -m build

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

kissut-1.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kissut-1.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file kissut-1.1.0.tar.gz.

File metadata

  • Download URL: kissut-1.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kissut-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ed5c7dd046875065d3a895a104573d8ace81c220d2f4291de781ff2a39605add
MD5 44b1c17c548b6318e2b728a6602dd994
BLAKE2b-256 f3433b241bdc96c75d16ea713e6032a546337d640cf5f68a5978dce605ff83bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for kissut-1.1.0.tar.gz:

Publisher: publish.yml on joemarchionna/kissut

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kissut-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: kissut-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kissut-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30938e6f13c6bd5946ae66546a84c53a3ededd1b5ace452370e06107276e5acf
MD5 ce58d84b883ed5e5499175d2d05226ba
BLAKE2b-256 8b90621e4d269adde5a45576e7e0a06498d58a69d679d0759454e72b7c5dc436

See more details on using hashes here.

Provenance

The following attestation bundles were made for kissut-1.1.0-py3-none-any.whl:

Publisher: publish.yml on joemarchionna/kissut

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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