punit


Namepunit JSON
Version 0.0.27 PyPI version JSON
download
home_pageNone
SummaryA modernized unit-test framework for Python.
upload_time2025-02-09 17:07:21
maintainerNone
docs_urlNone
authorNone
requires_python>=3.12
licenseNone
keywords test unittest unit-test
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            `pUnit` is a modernized unit-testing framework for Python, inspired by xUnit.

This README is only a high-level introduction to **pUnit**. For more detailed documentation, please view the official docs at [https://pUnit.readthedocs.io](https://pUnit.readthedocs.io).

## Command-Line Usage

Running pUnit with no arguments will perform test auto-discovery and execution:

```bash
python3 -m punit
```

By default it will look for tests in the `tests/` directory. You can override this behavior by providing a `--test-package` argument:

```bash
python3 -m punit --test-package elsewhere
```

In the above example, test discovery will instead occur in an `elsewhere/` directory.

### Report Generation

`pUnit` can generate "Test Results" reports. Currently supporting HTML and jUnit formats:

```bash
python3 -m punit --report html
```

Reports can also be output to a file where they can be lifted as part of a CI/CD pipeline and stored as an artifact or used for other purposes:

```bash
python3 -m punit --report junit --output results.xml
```

### Syntax Help

There are more options available, passing a `--help` argument will print help text:

```bash
python3 -m punit --help
```
Outputs:
```plaintext
Usage: python3 -m punit [-h|--help]
                        [-q|--quiet] [-v|--verbose]
                        [-f|--failfast]
                        [-p|--test-package NAME]
                        [-i|--include PATTERN]
                        [-e|--exclude PATTERN]
                        [-t|--filter PATTERN]
                        [-w|--workdir DIRECTORY]
                        [-n|--no-default-patterns]
                        [-r|--report {junit|json}]
                        [-o|--output FILENAME]

Options:
    -h, --help           Show this help text and exit
    -q, --quiet          Quiet output
    -v, --verbose        Verbose output
    -f, --failfast       Stop on first failure or error
    -p, --test-package NAME
        Use NAME as the test package, all tests should
        be locatable as modules in the named package.
        Default: 'tests'
    -i, --include PATTERN
        Include any tests matching PATTERN
        Default: '*.py'
    -e, --exclude PATTERN
        Exclude any tests matching PATTERN, overriding --include
        Default: '__*__' (dunder files), '/.*/' (dot-directories)
    -t, --filter PATTERN
        Only execute tests matching PATTERN
        Default: '*'
    -w, --working-directory DIRECTORY
        Working directory (defaults to start directory)
    -n, --no-default-patterns
        Do not apply any default include/exclude patterns.
    -r, --report {html|junit}
        Generate a report to stdout using either an "html"
        or "junit" format. When generating a report to stdout
        all other output is suppressed, unless `--output`
        is also specified.
    -o, --output FILENAME
        If `--report` is used, instead of writing to stdout
        write to FILENAME. In this case `--report` does not
        suppress any program output.
```

## Writing Tests

You can write tests as functions, class methods, instance methods, or static methods with all forms offering identical functionality. You can also utilize async/await syntax without any additional overhead.

**pUnit** is based upon the fundamental concepts of `Facts` and `Theories`. These are codified using decorators, aptly named `@fact` and `@theory`. Consider these examples:

```python

from punit import *

@fact
async def MyLibrary_WhenInitialized_TouchMustReturnTrue:
    mylib = MyLibrary()
    mylib.initialize()
    await asyncio.sleep(1)
    assert mylib.touch(), "Expecting touch() to return true, because initialize() was called."

class MyTestFixture:

    def calc(number:int|None = None):
        if number == None:
            raise Exception('Invalid value "None".')
        return number * number

    @theory
    @inlinedata(0, 0)
    @inlinedata(1, 1)
    @inlinedata(2, 4)
    @inlinedata(3, 9)
    @inlinedata(5, 25)
    @inlinedata(8, 64)
    def verifyCalcAssumptions(self, number:int, expected:int) -> None:
        assert expected == self.calc(number)

    @fact
    def verifyCalcErrorCondition(self) -> None:
        from punit.exceptions import raises
        # assert errors are raised, or not
        def calc_None():
            self.calc(None)
        def calc_1()
            self.calc(1)
        assert raises[Exception](calc_None)
        assert not raises[Exception](calc_1)
```

Because `pUnit` is a decorative framework you are afforded the utmost freedom in how you structure and implement your tests.

Unlike other testing frameworks, the names you use for functions, classes, and methods is not relevant. There is no requirement to inherit classes from specific abstract/base classes for any particular functionality. There is no requirement that your tests be organized into modules with `__init__.py` files.

You will want to take particular note of the `--exclude` command-line parameter which allows you to restrict what `pUnit` will consider to be a valid test file. While the default behavior will fit 99% of use-cases, you _can_ exercise more control over the discovery process.

## Vision, Future, and LTS

The long-term vision is to provide both imperative and declarative syntaxes for testing while keeping `pUnit` as simple as possible in its implementation.

`pUnit` is a Python 3.12+ package and there are no plans to backport it to earlier versions of Python, however, user contributions to support backward compatibility _will be accepted_ when it makes sense.

As Python progresses so will `pUnit` and SEMVER rules will be respected to provide developers with assurance that a major version of `pUnit` is fit for a particular purpose, thus, if there is ever a breaking change in Python that requires a breaking change in `pUnit` you can expect `pUnit` versioning to reflect this.

In any situation where an undocumented feature may be used maintainers will actively keep watch on deprecation notices and removals, will clearly identify these dependencies in the docs, and most importantly will provide a LTS alternative to any undocumented feature or such features will not be included in `pUnit`.

With respect to long-term support, we will commit to maintaining major versions for 3 years from the date they were superceded by a new major version. This should align well with Python Core Development.

## Contact

You can reach me on [Discord](https://discordapp.com/users/307684202080501761) or [open an Issue on Github](https://github.com/wilson0x4d/punit/issues/new/choose).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "punit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": "test, unittest, unit-test",
    "author": null,
    "author_email": "Shaun Wilson <mrshaunwilson@msn.com>",
    "download_url": "https://files.pythonhosted.org/packages/a3/1b/84c04c5b349dbb849ac4d3692361d5959efa26d8364d9c831c92bdb202f2/punit-0.0.27.tar.gz",
    "platform": null,
    "description": "`pUnit` is a modernized unit-testing framework for Python, inspired by xUnit.\n\nThis README is only a high-level introduction to **pUnit**. For more detailed documentation, please view the official docs at [https://pUnit.readthedocs.io](https://pUnit.readthedocs.io).\n\n## Command-Line Usage\n\nRunning pUnit with no arguments will perform test auto-discovery and execution:\n\n```bash\npython3 -m punit\n```\n\nBy default it will look for tests in the `tests/` directory. You can override this behavior by providing a `--test-package` argument:\n\n```bash\npython3 -m punit --test-package elsewhere\n```\n\nIn the above example, test discovery will instead occur in an `elsewhere/` directory.\n\n### Report Generation\n\n`pUnit` can generate \"Test Results\" reports. Currently supporting HTML and jUnit formats:\n\n```bash\npython3 -m punit --report html\n```\n\nReports can also be output to a file where they can be lifted as part of a CI/CD pipeline and stored as an artifact or used for other purposes:\n\n```bash\npython3 -m punit --report junit --output results.xml\n```\n\n### Syntax Help\n\nThere are more options available, passing a `--help` argument will print help text:\n\n```bash\npython3 -m punit --help\n```\nOutputs:\n```plaintext\nUsage: python3 -m punit [-h|--help]\n                        [-q|--quiet] [-v|--verbose]\n                        [-f|--failfast]\n                        [-p|--test-package NAME]\n                        [-i|--include PATTERN]\n                        [-e|--exclude PATTERN]\n                        [-t|--filter PATTERN]\n                        [-w|--workdir DIRECTORY]\n                        [-n|--no-default-patterns]\n                        [-r|--report {junit|json}]\n                        [-o|--output FILENAME]\n\nOptions:\n    -h, --help           Show this help text and exit\n    -q, --quiet          Quiet output\n    -v, --verbose        Verbose output\n    -f, --failfast       Stop on first failure or error\n    -p, --test-package NAME\n        Use NAME as the test package, all tests should\n        be locatable as modules in the named package.\n        Default: 'tests'\n    -i, --include PATTERN\n        Include any tests matching PATTERN\n        Default: '*.py'\n    -e, --exclude PATTERN\n        Exclude any tests matching PATTERN, overriding --include\n        Default: '__*__' (dunder files), '/.*/' (dot-directories)\n    -t, --filter PATTERN\n        Only execute tests matching PATTERN\n        Default: '*'\n    -w, --working-directory DIRECTORY\n        Working directory (defaults to start directory)\n    -n, --no-default-patterns\n        Do not apply any default include/exclude patterns.\n    -r, --report {html|junit}\n        Generate a report to stdout using either an \"html\"\n        or \"junit\" format. When generating a report to stdout\n        all other output is suppressed, unless `--output`\n        is also specified.\n    -o, --output FILENAME\n        If `--report` is used, instead of writing to stdout\n        write to FILENAME. In this case `--report` does not\n        suppress any program output.\n```\n\n## Writing Tests\n\nYou can write tests as functions, class methods, instance methods, or static methods with all forms offering identical functionality. You can also utilize async/await syntax without any additional overhead.\n\n**pUnit** is based upon the fundamental concepts of `Facts` and `Theories`. These are codified using decorators, aptly named `@fact` and `@theory`. Consider these examples:\n\n```python\n\nfrom punit import *\n\n@fact\nasync def MyLibrary_WhenInitialized_TouchMustReturnTrue:\n    mylib = MyLibrary()\n    mylib.initialize()\n    await asyncio.sleep(1)\n    assert mylib.touch(), \"Expecting touch() to return true, because initialize() was called.\"\n\nclass MyTestFixture:\n\n    def calc(number:int|None = None):\n        if number == None:\n            raise Exception('Invalid value \"None\".')\n        return number * number\n\n    @theory\n    @inlinedata(0, 0)\n    @inlinedata(1, 1)\n    @inlinedata(2, 4)\n    @inlinedata(3, 9)\n    @inlinedata(5, 25)\n    @inlinedata(8, 64)\n    def verifyCalcAssumptions(self, number:int, expected:int) -> None:\n        assert expected == self.calc(number)\n\n    @fact\n    def verifyCalcErrorCondition(self) -> None:\n        from punit.exceptions import raises\n        # assert errors are raised, or not\n        def calc_None():\n            self.calc(None)\n        def calc_1()\n            self.calc(1)\n        assert raises[Exception](calc_None)\n        assert not raises[Exception](calc_1)\n```\n\nBecause `pUnit` is a decorative framework you are afforded the utmost freedom in how you structure and implement your tests.\n\nUnlike other testing frameworks, the names you use for functions, classes, and methods is not relevant. There is no requirement to inherit classes from specific abstract/base classes for any particular functionality. There is no requirement that your tests be organized into modules with `__init__.py` files.\n\nYou will want to take particular note of the `--exclude` command-line parameter which allows you to restrict what `pUnit` will consider to be a valid test file. While the default behavior will fit 99% of use-cases, you _can_ exercise more control over the discovery process.\n\n## Vision, Future, and LTS\n\nThe long-term vision is to provide both imperative and declarative syntaxes for testing while keeping `pUnit` as simple as possible in its implementation.\n\n`pUnit` is a Python 3.12+ package and there are no plans to backport it to earlier versions of Python, however, user contributions to support backward compatibility _will be accepted_ when it makes sense.\n\nAs Python progresses so will `pUnit` and SEMVER rules will be respected to provide developers with assurance that a major version of `pUnit` is fit for a particular purpose, thus, if there is ever a breaking change in Python that requires a breaking change in `pUnit` you can expect `pUnit` versioning to reflect this.\n\nIn any situation where an undocumented feature may be used maintainers will actively keep watch on deprecation notices and removals, will clearly identify these dependencies in the docs, and most importantly will provide a LTS alternative to any undocumented feature or such features will not be included in `pUnit`.\n\nWith respect to long-term support, we will commit to maintaining major versions for 3 years from the date they were superceded by a new major version. This should align well with Python Core Development.\n\n## Contact\n\nYou can reach me on [Discord](https://discordapp.com/users/307684202080501761) or [open an Issue on Github](https://github.com/wilson0x4d/punit/issues/new/choose).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A modernized unit-test framework for Python.",
    "version": "0.0.27",
    "project_urls": {
        "Documentation": "https://punit.readthedocs.io/",
        "Homepage": "https://github.com/wilson0x4d/punit",
        "Repository": "https://github.com/wilson0x4d/punit.git"
    },
    "split_keywords": [
        "test",
        " unittest",
        " unit-test"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ee978756d5a62ca136f1ef2e769adfd8a6022074892d4c3b57a7be0fa2d95407",
                "md5": "6070b8b06142a64ef040b10f8c57a3f7",
                "sha256": "1f54b055f960d0b5c2a4dd04639754c62e1663682526898e4017395526899e10"
            },
            "downloads": -1,
            "filename": "punit-0.0.27-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6070b8b06142a64ef040b10f8c57a3f7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12",
            "size": 21782,
            "upload_time": "2025-02-09T17:07:20",
            "upload_time_iso_8601": "2025-02-09T17:07:20.313098Z",
            "url": "https://files.pythonhosted.org/packages/ee/97/8756d5a62ca136f1ef2e769adfd8a6022074892d4c3b57a7be0fa2d95407/punit-0.0.27-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a31b84c04c5b349dbb849ac4d3692361d5959efa26d8364d9c831c92bdb202f2",
                "md5": "c4b7a02e691ed706a926ea11d0f81c15",
                "sha256": "f3f937006cbb14c219e15120d5cbb602a8e8c513aa35b6d2fcce041bd868a986"
            },
            "downloads": -1,
            "filename": "punit-0.0.27.tar.gz",
            "has_sig": false,
            "md5_digest": "c4b7a02e691ed706a926ea11d0f81c15",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 19137,
            "upload_time": "2025-02-09T17:07:21",
            "upload_time_iso_8601": "2025-02-09T17:07:21.401257Z",
            "url": "https://files.pythonhosted.org/packages/a3/1b/84c04c5b349dbb849ac4d3692361d5959efa26d8364d9c831c92bdb202f2/punit-0.0.27.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-09 17:07:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "wilson0x4d",
    "github_project": "punit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "punit"
}
        
Elapsed time: 1.91295s