Pymox#

Python mocking on steroids

Release 1.4.1+d20240127 (What’s new?)


If you’d like more information on why Pymox, check out our summary.

Otherwise, let’s dive right in!

New Elegant Way#

# conftest.py
pytest_plugins = ("mox.testing.pytest_mox",)


# test.py
from mox import expect, stubout


class TestOs:
     def test_getcwd(self):
        with stubout(os, 'getcwd') as m_getcwd, expect:
            m_getcwd.to_be.called_with().and_return('/mox/path')

        assert os.getcwd() == '/mox/path'
        mox.verify(m_getcwd)

Classic Way#

import mox
import os

class TestOs:
    def test_getcwd(self):
        m = mox.Mox()

        m.stubout(os, 'getcwd')
        # calls
        os.getcwd().returns('/mox/path')

        m.replay_all()
        assert os.getcwd() == '/mox/path'
        m.verify_all()


if __name__ == '__main__':
    import unittest
    unittest.main()

Jurassic Way#

import mox
import os


class TestOs(mox.MoxTestBase):
    def test_getcwd(self):
        self.mox.StubOutWithMock(os, 'getcwd')
        # calls
        os.getcwd().AndReturn('/mox/path')

        self.mox.ReplayAll()
        self.assertEqual(os.getcwd(), '/mox/path')
        self.mox.VerifyAll()


if __name__ == '__main__':
    import unittest
    unittest.main()

Basics#

The first chapters shows the basics of Pymox and the features you’ll most likely to use.

Guides#

The following chapters give a broad explanation about Pymox features and how to use them.

Pymox in Practice#

The following chapters deal with considerations of using Pymox in the real world.

API Reference#

Indices and tables#