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:

    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.0.0.tar.gz (9.6 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.0.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kissut-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c719484609b1658446fe0635e7ab15e08da0c79b1308879c2c541567560be220
MD5 f5dc11c44d47b4f90a139aecb95c0651
BLAKE2b-256 bea28cafb625427a34236930fba980e48f3458f8975801f17812fede0e9ce1af

See more details on using hashes here.

Provenance

The following attestation bundles were made for kissut-1.0.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.0.0-py3-none-any.whl.

File metadata

  • Download URL: kissut-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f432b661b56362d472fb3c6e596c7807b82fefe1bbc1c21c45d48afc75ad93b
MD5 61c42bc1f26c64dc3d6f55eb9aaa7379
BLAKE2b-256 256ca3d1b02e2c4aad3ba998daa5e3033d3a574e147c9fb76bc76b363bb61898

See more details on using hashes here.

Provenance

The following attestation bundles were made for kissut-1.0.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