Knapsack Pro

unittest vs pytest comparison of testing frameworks
What are the differences between unittest and pytest?

unittest

https://docs.Python.org/3/library/unittest.html

pytest

https://docs.pytest.org/en/latest/
Programming language

Python

Python

Category

Unit Testing

Unit Testing

General info

unittest is a unit testing framework for Python

The unittest test framework is Python’s xUnit style framework. It is a standard module that is bundled with Python and supports the automation and aggregation of tests and common setup and shutdown code for them.

Pytest is the TDD 'all in one' testing framework for Python

Pytest is a powerful Python testing framework that can test all and levels of software. It is considered by many to be the best testing framework in Python with many projects on the internet having switched to it from other frameworks, including Mozilla and Dropbox. This is due to its many powerful features such as ‘assert‘ rewriting, a third-party plugin model and a powerful yet simple fixture model.
xUnit
Set of frameworks originating from SUnit (Smalltalk's testing framework). They share similar structure and functionality.

Yes

unittest is a xUnit style frameworkfor Python, it was previously called PyUnit.

No

Client-side
Allows testing code execution on the client, such as a web browser

Yes

Front-end functionality and behaviour can be tested by unittest.

Yes

pytest can test any part of the stack including front-end components
Server-side
Allows testing the bahovior of a server-side code

Yes

Since the webserver funtionalities have their own features and each feature has its own functions, we can write tests with unittest to test each function

Yes

pytest is powerful enough to test database and server components and functionality
Fixtures
Allows defining a fixed, specific states of data (fixtures) that are test-local. This ensures specific environment for a single test

Yes

By use of the 'setUp()' function which is called to prepare the test fixture

Yes

Pytest has a powerful yet simple fixture model that is unmatched in any other testing framework.
Group fixtures
Allows defining a fixed, specific states of data for a group of tests (group-fixtures). This ensures specific environment for a given group of tests.

Yes

unittest allows you to group your initialization code into a setUp function and clean up code in a tearDown function

Yes

Pytest's powerful fixture model allows grouping of fixtures
Generators
Supports data generators for tests. Data generators generate input data for test. The test is then run for each input data produced in this way.

Yes

unittest contains generator methods in the module 'unittest.TestCase'

Yes

pytest has a hook function called pytest_generate_tests hook which is called when collecting a test function and one can use it to generate data
Licence
Licence type governing the use and redistribution of the software

MIT License

MIT License

Mocks
Mocks are objects that simulate the behavior of real objects. Using mocks allows testing some part of the code in isolation (with other parts mocked when needed)

Yes

Mocks are available from the library unittest.mock which allows you to replace parts of your system under test with mock objects

Yes

By either using unittest.mock or using pytest-mock a thin wrapper that provides mock functionality for pytest
Grouping
Allows organizing tests in groups

Yes

One can build suites either manually or use test discovery to build the suite automatically by scanning a directory

Yes

Tests can be grouped with pytest by use of markers which are applied to various tests and one can run tests with the marker applied
Other
Other useful information about the testing framework