vutils-testing


Namevutils-testing JSON
Version 2.0.3 PyPI version JSON
download
home_pagehttps://github.com/i386x/vutils-testing
SummaryAuxiliary library for writing tests
upload_time2024-10-05 10:55:28
maintainerNone
docs_urlNone
authorJiří Kučera
requires_python<4,>=3.8
licenseMIT
keywords testing mocking unit testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Coverage Status](https://coveralls.io/repos/github/i386x/vutils-testing/badge.svg?branch=main)](https://coveralls.io/github/i386x/vutils-testing?branch=main)
![CodeQL](https://github.com/i386x/vutils-testing/actions/workflows/codeql.yml/badge.svg)

# vutils-testing: Auxiliary Library for Writing Tests

This package provides a set of tools that help with writing tests. It helps
with creating test data and types, mocking objects, patching, and verifying
test results.

## Installation

To install the package, type
```sh
$ pip install vutils-testing
```

## How to Use

For more details, please follow the subsections below.

### Type Factories

Sometimes tests require new types to be defined. To do this,
`vutils.testing.utils` provides `make_type` function, which is a wrapper of
`type`:
```python
# Create type derived directly from object:
my_type = make_type("MyType")

# Create class derived directly from Exception:
my_error = make_type("MyError", Exception)

# Create class derived from A and B:
my_class = make_type("MyClass", (A, B))

# Create class derived from A with foo member:
my_another_class = make_type("MyAnotherClass", A, {"foo": 42})

# Create class derived from object with foo member:
my_test_class = make_type("MyTestClass", members={"foo": 42})

# Key-value arguments other than bases and members are passed to
# __init_subclass__:
my_fourth_class = make_type("MyFourthClass", bases=A, foo=42)
```

### Mocking Objects and Patching

`make_mock`, `make_callable`, and `PatcherFactory` from `vutils.testing.mock`
allow to create mock objects and patching things.

`make_mock(*args, **kwargs)` is a shortcut for `unittest.mock.Mock`

`make_callable(x)` creates also instance of `unittest.mock.Mock`, but it
specifies its function-related behavior: if `x` is callable, it is used to do a
side-effect, otherwise it is used as the return value.
```python
# func_a() returns 3
func_a = make_callable(3)

container = []

# func_b() appends 42 to container
func_b = make_callable(lambda *x, **y: container.append(42))

# func_c() returns func_b
func_c = make_callable(lambda *x, **y: func_b)
```

`PatcherFactory` allows to use `unittest.mock.patch` multiple-times without
need of nested `with` statements. When instantiated, `setup` method is called.
`setup` method, implemented in the subclass, then may define set of patcher
specifications via `add_spec` method:
```python
class MyPatcher(PatcherFactory):

    @staticmethod
    def setup_foo(mock):
        mock.foo = "foo"

    @staticmethod
    def setup_baz(baz):
        baz["quux"] = 42

    def setup(self):
        self.baz = {}
        # When self.patch() is called:
        # - create a mock object, apply setup_foo on it, and patch foopkg.foo
        #   with it:
        self.add_spec("foopkg.foo", self.setup_foo)
        # - patch foopkg.bar with 42:
        self.add_spec("foopkg.bar", new=42)
        # - apply setup_baz on baz and patch foopkg.baz with it (create=True
        #   and other possible key-value arguments are passed to
        #   unittest.mock.patch):
        self.add_spec("foopkg.baz", self.setup_baz, new=self.baz, create=True)

patcher = MyPatcher()

with patcher.patch():
   # Patches are applied in order as specified by add_spec and reverted in
   # reverse order.
   do_something()
```

### Deferred Instance Initialization

Patching may take no effect if the patched object appears in constructor and
this constructor is called outside of patcher context. `LazyInstance` from
`vutils.testing.utils` can defer initialization up to the time of method call:
```python
class StderrWriter:
    def __init__(self):
        self.stream = sys.stderr

    def write(self, text):
        self.stream.write(text)

class StderrPatcher(PatcherFactory):
    def setup(self):
        self.stream = io.StringIO
        self.add_spec("sys.stderr", new=self.stream)

class MyTestCase(TestCase):
    def test_deferred_initialization(self):
        writerA = StderrWriter()
        writerB = LazyInstance(StderrWriter).create()
        patcher = StderrPatcher()

        # Patch sys.stderr:
        with patcher.patch():
            # Write Hello! to standard error output:
            writerA.write("Hello!\n")
            # Write Hi! to StringIO instance:
            writerB.write("Hi!\n")
```

### Deferred `assertRaises`

Sometimes there are callable objects with a very similar prototypes and
behavior so they can be run and checked with one universal function. However,
if one of them raises an exception under specific circumstances, this must be
also handled by the universal function, which adds to its complexity. For this
reason, `vutils.testing.utils` introduces `AssertRaises` class which wraps
exception raising assertions:
```python
class FooError(Exception):
    detail = "foo"

def func_a(obj):
    obj.foo = 42

def func_b(obj):
    func_a(obj)
    raise FooError()

def Foo(TestCase):
    def run_and_check(self, func):
        obj = make_mock()
        func(obj)
        self.assertEqual(obj.foo, 42)

    def test_func(self):
        wfunc_b = AssertRaises(self, func_b, FooError)

        self.run_and_check(func_a)
        # Catch and store FooError:
        self.run_and_check(wfunc_b)
        # Check the caught exception:
        self.assertEqual(wfunc_b.get_exception().detail, "foo")
```

### Enhanced `TestCase`

Module `vutils.testing.testcase` provides `TestCase` which is a subclass of
`unittest.TestCase` extended about these methods:

* `assert_called_with` - assert that the mock object has been called once with
  the specified arguments and then reset it:
  ```python
  class ExampleTestCase(TestCase):
      def test_assert_called_with(self):
          mock = make_mock(["foo"])

          mock.foo(1, 2, bar=3)
          self.assert_called_with(mock, 1, 2, bar=3)

          mock.foo(4)
          self.assert_called_with(mock, 4)
  ```
* `assert_not_called` - assert that the mock object has not been called:
  ```python
  class ExampleTestCase(TestCase):
      def test_assert_not_called(self):
          mock = make_mock(["foo"])

          self.assert_not_called(mock.foo)
  ```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/i386x/vutils-testing",
    "name": "vutils-testing",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8",
    "maintainer_email": null,
    "keywords": "testing, mocking, unit testing",
    "author": "Ji\u0159\u00ed Ku\u010dera",
    "author_email": "sanczes@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/56/df/c028d6a7f1cf36b8b2b3ed27471864981be470ababdec54c5ea25e9d8b36/vutils_testing-2.0.3.tar.gz",
    "platform": "any",
    "description": "[![Coverage Status](https://coveralls.io/repos/github/i386x/vutils-testing/badge.svg?branch=main)](https://coveralls.io/github/i386x/vutils-testing?branch=main)\n![CodeQL](https://github.com/i386x/vutils-testing/actions/workflows/codeql.yml/badge.svg)\n\n# vutils-testing: Auxiliary Library for Writing Tests\n\nThis package provides a set of tools that help with writing tests. It helps\nwith creating test data and types, mocking objects, patching, and verifying\ntest results.\n\n## Installation\n\nTo install the package, type\n```sh\n$ pip install vutils-testing\n```\n\n## How to Use\n\nFor more details, please follow the subsections below.\n\n### Type Factories\n\nSometimes tests require new types to be defined. To do this,\n`vutils.testing.utils` provides `make_type` function, which is a wrapper of\n`type`:\n```python\n# Create type derived directly from object:\nmy_type = make_type(\"MyType\")\n\n# Create class derived directly from Exception:\nmy_error = make_type(\"MyError\", Exception)\n\n# Create class derived from A and B:\nmy_class = make_type(\"MyClass\", (A, B))\n\n# Create class derived from A with foo member:\nmy_another_class = make_type(\"MyAnotherClass\", A, {\"foo\": 42})\n\n# Create class derived from object with foo member:\nmy_test_class = make_type(\"MyTestClass\", members={\"foo\": 42})\n\n# Key-value arguments other than bases and members are passed to\n# __init_subclass__:\nmy_fourth_class = make_type(\"MyFourthClass\", bases=A, foo=42)\n```\n\n### Mocking Objects and Patching\n\n`make_mock`, `make_callable`, and `PatcherFactory` from `vutils.testing.mock`\nallow to create mock objects and patching things.\n\n`make_mock(*args, **kwargs)` is a shortcut for `unittest.mock.Mock`\n\n`make_callable(x)` creates also instance of `unittest.mock.Mock`, but it\nspecifies its function-related behavior: if `x` is callable, it is used to do a\nside-effect, otherwise it is used as the return value.\n```python\n# func_a() returns 3\nfunc_a = make_callable(3)\n\ncontainer = []\n\n# func_b() appends 42 to container\nfunc_b = make_callable(lambda *x, **y: container.append(42))\n\n# func_c() returns func_b\nfunc_c = make_callable(lambda *x, **y: func_b)\n```\n\n`PatcherFactory` allows to use `unittest.mock.patch` multiple-times without\nneed of nested `with` statements. When instantiated, `setup` method is called.\n`setup` method, implemented in the subclass, then may define set of patcher\nspecifications via `add_spec` method:\n```python\nclass MyPatcher(PatcherFactory):\n\n    @staticmethod\n    def setup_foo(mock):\n        mock.foo = \"foo\"\n\n    @staticmethod\n    def setup_baz(baz):\n        baz[\"quux\"] = 42\n\n    def setup(self):\n        self.baz = {}\n        # When self.patch() is called:\n        # - create a mock object, apply setup_foo on it, and patch foopkg.foo\n        #   with it:\n        self.add_spec(\"foopkg.foo\", self.setup_foo)\n        # - patch foopkg.bar with 42:\n        self.add_spec(\"foopkg.bar\", new=42)\n        # - apply setup_baz on baz and patch foopkg.baz with it (create=True\n        #   and other possible key-value arguments are passed to\n        #   unittest.mock.patch):\n        self.add_spec(\"foopkg.baz\", self.setup_baz, new=self.baz, create=True)\n\npatcher = MyPatcher()\n\nwith patcher.patch():\n   # Patches are applied in order as specified by add_spec and reverted in\n   # reverse order.\n   do_something()\n```\n\n### Deferred Instance Initialization\n\nPatching may take no effect if the patched object appears in constructor and\nthis constructor is called outside of patcher context. `LazyInstance` from\n`vutils.testing.utils` can defer initialization up to the time of method call:\n```python\nclass StderrWriter:\n    def __init__(self):\n        self.stream = sys.stderr\n\n    def write(self, text):\n        self.stream.write(text)\n\nclass StderrPatcher(PatcherFactory):\n    def setup(self):\n        self.stream = io.StringIO\n        self.add_spec(\"sys.stderr\", new=self.stream)\n\nclass MyTestCase(TestCase):\n    def test_deferred_initialization(self):\n        writerA = StderrWriter()\n        writerB = LazyInstance(StderrWriter).create()\n        patcher = StderrPatcher()\n\n        # Patch sys.stderr:\n        with patcher.patch():\n            # Write Hello! to standard error output:\n            writerA.write(\"Hello!\\n\")\n            # Write Hi! to StringIO instance:\n            writerB.write(\"Hi!\\n\")\n```\n\n### Deferred `assertRaises`\n\nSometimes there are callable objects with a very similar prototypes and\nbehavior so they can be run and checked with one universal function. However,\nif one of them raises an exception under specific circumstances, this must be\nalso handled by the universal function, which adds to its complexity. For this\nreason, `vutils.testing.utils` introduces `AssertRaises` class which wraps\nexception raising assertions:\n```python\nclass FooError(Exception):\n    detail = \"foo\"\n\ndef func_a(obj):\n    obj.foo = 42\n\ndef func_b(obj):\n    func_a(obj)\n    raise FooError()\n\ndef Foo(TestCase):\n    def run_and_check(self, func):\n        obj = make_mock()\n        func(obj)\n        self.assertEqual(obj.foo, 42)\n\n    def test_func(self):\n        wfunc_b = AssertRaises(self, func_b, FooError)\n\n        self.run_and_check(func_a)\n        # Catch and store FooError:\n        self.run_and_check(wfunc_b)\n        # Check the caught exception:\n        self.assertEqual(wfunc_b.get_exception().detail, \"foo\")\n```\n\n### Enhanced `TestCase`\n\nModule `vutils.testing.testcase` provides `TestCase` which is a subclass of\n`unittest.TestCase` extended about these methods:\n\n* `assert_called_with` - assert that the mock object has been called once with\n  the specified arguments and then reset it:\n  ```python\n  class ExampleTestCase(TestCase):\n      def test_assert_called_with(self):\n          mock = make_mock([\"foo\"])\n\n          mock.foo(1, 2, bar=3)\n          self.assert_called_with(mock, 1, 2, bar=3)\n\n          mock.foo(4)\n          self.assert_called_with(mock, 4)\n  ```\n* `assert_not_called` - assert that the mock object has not been called:\n  ```python\n  class ExampleTestCase(TestCase):\n      def test_assert_not_called(self):\n          mock = make_mock([\"foo\"])\n\n          self.assert_not_called(mock.foo)\n  ```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Auxiliary library for writing tests",
    "version": "2.0.3",
    "project_urls": {
        "Bug Reports": "https://github.com/i386x/vutils-testing/issues",
        "Homepage": "https://github.com/i386x/vutils-testing",
        "Source": "https://github.com/i386x/vutils-testing"
    },
    "split_keywords": [
        "testing",
        " mocking",
        " unit testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9b7568ab8df22b65759b5a9e44e202d6ac4790c085809bdcd580e35cfb6a53dc",
                "md5": "76cee3e08383e3cc56b4974c37d601a3",
                "sha256": "b93f1555503329756db205a88fbf92b5ec84eb885b721e4bda9379e31fcebb05"
            },
            "downloads": -1,
            "filename": "vutils_testing-2.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "76cee3e08383e3cc56b4974c37d601a3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8",
            "size": 12466,
            "upload_time": "2024-10-05T10:55:26",
            "upload_time_iso_8601": "2024-10-05T10:55:26.900752Z",
            "url": "https://files.pythonhosted.org/packages/9b/75/68ab8df22b65759b5a9e44e202d6ac4790c085809bdcd580e35cfb6a53dc/vutils_testing-2.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "56dfc028d6a7f1cf36b8b2b3ed27471864981be470ababdec54c5ea25e9d8b36",
                "md5": "615d205f27973146528f2d03a2316851",
                "sha256": "83ed12fe0857ad7d82bac995cb5312ce7cc1bb7d136c6ce0dc526e67161ace96"
            },
            "downloads": -1,
            "filename": "vutils_testing-2.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "615d205f27973146528f2d03a2316851",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 18006,
            "upload_time": "2024-10-05T10:55:28",
            "upload_time_iso_8601": "2024-10-05T10:55:28.222689Z",
            "url": "https://files.pythonhosted.org/packages/56/df/c028d6a7f1cf36b8b2b3ed27471864981be470ababdec54c5ea25e9d8b36/vutils_testing-2.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-05 10:55:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "i386x",
    "github_project": "vutils-testing",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "vutils-testing"
}
        
Elapsed time: 3.95167s