ansible / ansible-documentation

Ansible community documentation
https://docs.ansible.com/
GNU General Public License v3.0
83 stars 492 forks source link

Unit testing documentation is out of date #81

Open tremble opened 2 years ago

tremble commented 2 years ago

Summary

Speaking to @mattclay, using the pytest framework for unit tests is preferred over unittest framework.

Both https://github.com/ansible/ansible/blob/devel/docs/docsite/rst/dev_guide/testing_units.rst and https://github.com/ansible/ansible/blob/devel/docs/docsite/rst/dev_guide/testing_units_modules.rst have a mix of both frameworks in the examples, the unittest examples should probably be migrated over to using pytest.

Issue Type

Documentation Report

Component Name

docs/docsite/rst/dev_guide/testing_units.rst

Ansible Version

n/a

Configuration

n/a

OS / Environment

n/a

Additional Information

If we want folks to prefer pytest over unittest, we need to update the docs to reflect this.

Code of Conduct

ansibot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

Aysha-py commented 2 years ago

@samccann is this issue available to be contributed to? Would love to work on it

tremble commented 2 years ago

@Aysha-py best option is to start the work and open a draft Pull Request. If you add fixes: ansible/ansible-documentation#81 to the PR description it will automatically link to this issue (and close the issue when the PR is merged). That link will show up and means folks know you're working on it.

Aysha-py commented 2 years ago

@tremble okay, can you give further clarification on updating these docs? Do I update the unittest docs to pytest? If so where can I access the content of pytest?

tremble commented 2 years ago

The current docs are linked in the original issue description. You'll find some examples doing things like

class TestMyModule(unittest.TestCase):
    def setUp(self):
        ...

unittest.TestCase isn't needed as the superclass (pytest doesn't require it), however setUp needs to be replaced with setup_method

There's some more information in the pytest docs: https://docs.pytest.org/en/latest/how-to/xunit_setup.html#method-and-function-level-setup-teardown

I also found https://www.jitsejan.com/moving-from-unittest-to-pytest which looks like it explains some of what's going on.

webknjaz commented 2 years ago

pytest doesn't require it

Moreover: it is harmful even to inherit from unittest.TestCase under the pytest runner because it prevents usage of many pytest features — https://docs.pytest.org/en/latest/how-to/unittest.html#pytest-features-in-unittest-testcase-subclasses.

Besides, with pytest fixtures, using classes is not really necessary — it's much cleaner to have setup and teardown in fixtures. And there's no need for an extra namespace for the test callables (a test module is already one).