naming20.rst

Overview

This documentation file, [naming20.rst](/projects/286/67223), explains the renaming conventions and namespace restructuring introduced in version 2.0 of the pytest testing framework. The primary purpose of this file is to guide users migrating from older versions of the `py` distribution (which included the `py.test` command line tool and Python namespace) to the new, simplified `pytest` module structure.

Historically, older versions exposed testing helpers and collection classes under the `py.test` namespace, which was somewhat nested and less intuitive. Version 2.0 refactors this by exposing the same API objects directly and flatly in the `pytest` namespace, promoting clearer, easier to use naming and import patterns.

This file serves as a concise reference for developers updating their test codebases to align with the new naming standards, ensuring compatibility with pytest 2.0 and later.

Detailed Explanation

Purpose and Functionality

Key Renaming Rules

The file enumerates the following critical renaming mappings:

Old Naming

New Naming

Notes

py.test.XYZ

pytest.XYZ

Direct rename

py.test.collect.XYZ

pytest.XYZ

Flattened from collect submodule

py.test.cmdline.main

`pytest.main`

Main entry point for CLI

Usage Examples

Old Style (Pre-2.0)

from py.test import raises
from py.test.collect import Collector
from py.test.cmdline import main

def test_example():
    with raises(ValueError):
        raise ValueError("error")

main()  # Run tests via CLI programmatically

New Style (2.0 and Later)

from pytest import raises
from pytest import Collector
from pytest import main

def test_example():
    with raises(ValueError):
        raise ValueError("error")

main()  # Run tests via CLI programmatically

Implementation Details

Interactions with Other System Components

Mermaid Diagram: Namespace Structure Transition

The following class diagram illustrates the namespace flattening and renaming of key components from the old `py.test` hierarchy to the new `pytest` flat namespace:

classDiagram
    class PyTest {
        <<namespace>>
        +XYZ
        +collect.XYZ
        +cmdline.main()
    }
    class PyTestCollect {
        <<namespace>>
        +XYZ
    }
    class PyTestCmdline {
        <<namespace>>
        +main()
    }
    class Pytest {
        <<namespace>>
        +XYZ
        +main()
    }
    PyTestCollect --|> PyTest
    PyTestCmdline --|> PyTest
    Pytest ..> PyTest : replaces
    Pytest ..> PyTestCollect : flattens
    Pytest ..> PyTestCmdline : flattens

This diagram depicts:

Summary

[naming20.rst](/projects/286/67223) is a concise guideline document intended for pytest users upgrading to version 2.0. It explains the shift from nested `py.test` namespaces to a simpler, flat `pytest` namespace, providing renaming rules and usage examples. The document encourages users to update imports globally for cleaner, more maintainable test code and ensures continuity via backward compatibility. The namespace flattening aligns with Pythonic design principles, improving the developer experience with pytest.


*End of [naming20.rst](/projects/286/67223) documentation.*