Skip to main content

Flake8-AAA

Travis build Read the Docs PyPI PyPI - Python Version flake8-aaa is licensed under the MIT License

A Flake8 plugin that checks Python tests follow the Arrange-Act-Assert pattern.


📝 Table of Contents

🧐 About

What is the Arrange-Act-Assert pattern?

“Arrange-Act-Assert” is a testing pattern that focuses each test on a single object’s behaviour. It’s also known as “AAA” and “3A”.

As the name suggests each test is broken down into three distinct parts separated by blank lines:

  • Arrange: Set up the object to be tested.

  • Act: Carry out an action on the object.

  • Assert: Check the expected results have occurred.

For example, a simple test on the behaviour of add +:

def test():
   x = 1
   y = 1

   result = x + y

   assert result == 2

As you can see, the Act block starts with result = and is separated from the Arrange and Assert blocks by blank lines. The test is focused - it only contains one add operation and no further additions occur.

Using AAA consistently makes it easier to find the Action in a test. It’s therefore always easy to see the object behaviour each test is focused on.

Further reading:

What is Flake8?

Flake8 is a command line utility for enforcing style consistency across Python projects. It wraps multiple style checking tools and also runs third-party checks provided by plugins, of which Flake8-AAA is one.

Further reading:

What does Flake8-AAA do?

Flake8-AAA extends Flake8 to check your Python tests match the AAA pattern.

It does this by adding the following checks to Flake8:

  • Every test has a single clear Act block.

  • Every Act block is distinguished from the code around it with a blank line above and below.

  • Arrange and Assert blocks do not contain additional blank lines.

In the future, Flake8-AAA will check that no test has become too complicated and that Arrange blocks do not contain assertions.

Checking your code with these simple formatting rules helps you write simple, consistently formatted tests that match the AAA pattern. They are most helpful if you call Flake8 regularly, for example when you save a file or before you run a test suite.

Further reading:

🏁 Getting Started

Prerequisites

Install Flake8 with pip:

$ pip install flake8

Installation

Install flake8-aaa:

$ pip install flake8-aaa

You can confirm that Flake8 recognises the plugin by checking its version string:

$ flake8 --version
3.7.9 (aaa: 0.9.0, mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) ...

The aaa: 0.9.0 part tells you that Flake8-AAA was installed successfully and its checks will be used by Flake8.

Further reading:

First run

Let’s check the good example from above. We expect Flake8 to return no errors:

$ curl https://raw.githubusercontent.com/jamescooke/flake8-aaa/master/examples/good/test_example.py > test_example.py
$ flake8 test_example.py

Silence - just what we wanted.

Now let’s see a failure from Flake8-AAA. We can use a bad example:

$ curl https://raw.githubusercontent.com/jamescooke/flake8-aaa/master/examples/bad/test.py > test.py
$ flake8 test.py
test.py:4:1: AAA01 no Act block found in test

🎈 Usage

Via Flake8

Since Flake8-AAA is primarily a Flake8 plugin, the majority of its usage is dependent on how you use Flake8. In general you can point it at your source code and test suite:

$ flake8 src tests

If you’re not already using Flake8 then you might consider:

  • Adding a hook to your code editor to run Flake8 when you save a file.

  • Adding a pre-commit hook to your source code manager to run Flake8 before you commit.

  • Running Flake8 before you execute your test suite - locally or in CI.

If you just want Flake8-AAA error messages you can filter errors returned by Flake8 with --select:

$ flake8 --select AAA tests

Further reading:

Via command line

Flake8-AAA also provides a command line interface. Although this is primarily for debugging, it can be used to check individual files if you don’t want to install Flake8.

$ python -m flake8_aaa [test_file]

Further reading:

⛏️ Compatibility

Flake8-AAA works with:

  • Pytest and unittest test suites.

  • Black and yapf formatted code.

  • Mypy and type-annotated code.

  • Latest versions of Python 3 (3.6, 3.7 and 3.8).

Further reading:

📕 Resources

Metadata

Release files for flake8-aaa 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-aaa 0.10.0
File Size Uploaded Provenance Actions
flake8-aaa-0.10.0.tar.gz 17.9 kB Without provenance Details for flake8-aaa-0.10.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flake8-aaa 0.10.0
File Interpreter ABI Platform Actions
flake8_aaa-0.10.0-py3-none-any.whl Python 3 none any Details for flake8_aaa-0.10.0-py3-none-any.whl

Total release size: 36.3 kB

Release files / flake8-aaa-0.10.0.tar.gz

Download URL flake8-aaa-0.10.0.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e176f79a791b0d2df900670e4b069bf50c237e18c9152aae86e5446405f16cef
BLAKE2b-256 checksum
How to use checksums
86333bc1285c4061a1d98be45b3c02b2943b093393f5aa5fdbe2c67be5b21e7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

Release files / flake8_aaa-0.10.0-py3-none-any.whl

Download URL flake8_aaa-0.10.0-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1558ce6607481079a57bbfbf7d34661f4831755f46fbc7dd2f19503e373919d8
BLAKE2b-256 checksum
How to use checksums
7dd5bd64de512e13c43cc8ff0accbb9aaed52d68912eb303070aeb69e79db877
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

Security for flake8-aaa 0.10.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page