Skip to main content

beannetworktester

Slow down, break or cut the real network in your tests: latency, packet loss, jitter, bandwidth limits, outages. For one server, one API or one process, on Windows and Linux, with pytest, Playwright for Python or any Python code.

def test_the_page_survives_a_slow_api(bad_network, page):
    bad_network(server="http://localhost:8000", latency=300, loss=2)
    page.goto("http://localhost:3000")
    ...

Nothing is mocked: your app, your browser and your server really talk over a slow network. The package carries Bean Network Tester's own program and runs it for the length of a test. On Windows it is the same signed program as the release.

Quick start

1. Install

pip install beannetworktester

Windows x64 or Linux x64 (glibc 2.39 or newer, such as Ubuntu 24.04), Python 3.10 or newer.

2. Get the rights. Changing the network needs administrator rights on Windows and root on Linux:

  • Windows: open the terminal with Run as administrator. GitHub's Windows runners already run elevated.
  • Linux: run the tests as root, or as a user who may run sudo without a password, as on GitHub's Ubuntu runners. Then only the program runs under sudo, and your tests do not.

3. Save this as test_slow.py and run pytest

import subprocess
import sys
import time
import urllib.request

import pytest


@pytest.fixture
def web_server():
    server = subprocess.Popen([sys.executable, "-m", "http.server", "8000"])
    time.sleep(1)                                    # give it a moment to listen
    yield "http://localhost:8000"
    server.terminate()


def test_a_page_takes_longer_on_a_slow_network(bad_network, web_server):
    bad_network(server=web_server, latency=500)      # every connection to it, +500 ms
    started = time.monotonic()
    urllib.request.urlopen(web_server).read()
    assert time.monotonic() - started >= 0.5

The test passes because the page really took half a second longer. When it ends, the network is back to normal.

No rights at hand? bad_network(simulate=True, latency=500) runs the program on made-up traffic, needs no rights and changes nothing on the network. It is for checking that the package is set up, not for slowing your app.

In pytest

The bad_network fixture starts a session when you call it and returns once the network is slow. The session stops when the test ends, whether it passed, failed or crashed:

def test_checkout_retries_when_the_api_is_slow(bad_network):
    bad_network(server="https://localhost:8443", latency=500, jitter=100)
    assert place_order().status == "paid"

To change the conditions in the middle of a test, stop the first session before you start the next one, or give one session a scenario (below):

def test_the_app_recovers_when_the_api_comes_back(bad_network):
    outage = bad_network(server="http://localhost:8000", loss=100)
    assert app_shows_offline_banner()
    outage.stop()                                    # the network is normal again
    assert app_hides_offline_banner()

Playwright for Python

Playwright for Python runs on pytest, so the fixture is the same:

from playwright.sync_api import expect


def test_the_spinner_shows_on_a_slow_network(bad_network, page):
    bad_network(server="http://localhost:3000", latency=800)
    page.goto("http://localhost:3000")
    expect(page.get_by_role("progressbar")).to_be_visible()

Chromium, Firefox and WebKit are all slowed the same way, and so are WebSockets: the network itself is slow, not the browser's idea of it.

Without pytest

from beannetworktester import bad_network

with bad_network(server="http://localhost:3000", loss=5, duration=120) as session:
    run_my_client()
print(session.summary["counters"]["scoped_seen"], "packets were in the aim")

bad_network(...) starts the session and returns it, and with stops it at the end. Without with, call session.stop(), which returns the summary.

What a session aims at

Every session aims at something, and only that traffic changes. Without an aim it would slow every connection of the machine, the test runner's own included, so bad_network refuses to start.

Aim What is slowed
server="http://localhost:3000" every connection to that address and port, from any program. Also "db.local:5432" or ("10.0.0.5", 443)
server=["http://localhost:3000", "localhost:8080"] several servers: every address of them with every port of them
process="self" this test process and every program it started, such as Playwright's browser
process=proc a process your test started: pass the subprocess.Popen itself, not its number
dst_port=8080 every connection of this machine to that port, at any address: an API on a known port
dst_ip="10.0.0.5" every connection to that address, any port. Also lists, ranges and CIDR such as "172.18.0.0/16"
target="myapi" processes by name or by process ID, in the program's syntax

server= is the first choice: it is exact on Windows and Linux. dst_ip, dst_port and target are the program's own aims, written in its syntax (lists, ranges, ! to leave something out). They reach everything that matches, so think about what else uses it:

  • dst_port=443 is every HTTPS connection of the machine: your browser, your editor, the CI runner's own connection. Aim at the server instead, or add process="self".
  • target="node" is every program whose name contains "node", your editor and the test runner included.
  • dst_ip="!10.0.0.5" is everything except that address.

Aims given together narrow each other. server= with process="self" slows only this test's connections to that server, and process="self" with dst_port=8080 only this test's connections to port 8080.

On Linux, process= and target reach a new connection up to about 0.2 seconds after it opens, so its first packets can go through untouched. Aim at the server when the first request counts.

What a session can do

Every setting is one of the program's flags, written with _ instead of -:

Setting What it does
latency=300 adds 300 ms to every packet
jitter=100 varies the delay by up to 100 ms either way
loss=5 drops 5% of the packets
down=256, up=64 limits download and upload to so many KB/s
flap_period=10, flap_down=30 the link goes down for 30% of every 10 seconds
rst_prob=2 tears 2% of TCP connections down with a reset
loss=100 cuts the connections in the aim off completely
block_port=5432 blocks the traffic to that port, inside the aim
preset="presets.3g" a named link profile, such as 3G or a satellite link
seed=42 the same random choices on every run

The program checks the settings itself, so a misspelled one fails the test with its message. The full list is on the command-line page and in BeanNetworkTester --help.

Conditions that change over time

A scenario is a JSON file of steps, each at a second of the session:

{"steps": [
  {"at": 0,  "settings": {"latency": 100}},
  {"at": 10, "settings": {"latency": 800, "loss": 10}},
  {"at": 20, "settings": {"loss": 100}}
]}
bad_network(server="http://localhost:3000", scenario="slow-then-down.json", duration=60)

The program's own scenarios work by name, such as scenario="scenarios/cafe-wifi.json" or "scenarios/mobile-lte-to-3g.json". A step may change how the network behaves, but not what server= or process= aim at: the helper reads the file first and refuses such a step, because it could widen the aim to the whole machine.

Safety

  • Never the whole machine by accident. A session without an aim does not start.
  • It ends with the test. The fixture stops it when the test ends. If the test process dies, the program sees its input close and stops at once. duration (300 seconds unless you give another) is the most a session may run in any case, and the test gets a BadNetworkWarning when it ran out first.
  • One at a time. Two sessions over the same traffic would add up. Parallel workers (pytest-xdist) take turns, each waiting up to wait seconds (600 unless you give another). A second session in the same test is refused at once.
  • It says when it did nothing. A session that slowed nothing, because nothing reached its aim, ends with a BadNetworkWarning.

When something goes wrong

What you see What to do
"needs administrator rights on Windows" open the terminal with Run as administrator
"needs root on Linux" run as root, or allow sudo without a password for this user
BadNetworkWarning: "the session slowed nothing" the app talks to another address than the aim: a container, the machine's network address instead of localhost, or a proxy. Aim at the address it really uses, or use process="self"
"Another bad_network session has been running" parallel workers wait for each other: give a longer wait=, or run those tests in one worker
"has not been stopped yet" stop the first session of the test before you start the next one
"runs the network on Windows x64 and Linux x64 only" on a Mac, run the tests in a Linux x86_64 container or virtual machine, or on a CI runner
"has no program in it" install with a 64-bit Python and a current pip
BadNetworkWarning: "ended by its duration" give a longer duration=
"the program refused the settings" the program's own words follow, and name the setting

The summary

Every session ends with the program's summary: session.summary, or stop()'s return value. In pytest it also goes into the test's user_properties, and from there into a JUnit XML report. The parts worth reading:

  • counters["seen"]: packets the program saw. counters["scoped_seen"]: those in the aim.
  • stop_reason: "stdin" when the test stopped it, "duration" when time ran out.
  • seed and repro_command: the command that repeats the same conditions with the program on its own.

Other options

  • duration=: the most seconds a session may run, 300 unless given, at most 86400.
  • wait=: seconds to wait for another session to end, 600 unless given.
  • start_timeout=: seconds to wait for the program to start, 60 unless given.
  • BEANNETWORKTESTER_PROGRAM in the environment points at another copy of the program, such as one installed with WinGet, or bean_network_tester.py from a checkout.

License

The package's own code, the part your tests import, is under the Mozilla Public License 2.0 (MPL-2.0), in LICENSE. You can use it in the tests of any project, closed-source ones included, and your tests stay yours. If you change one of the package's own files and pass it on, that file stays under MPL-2.0.

The program the package carries and starts is a separate program, under GPL-3.0-only. Its license and third-party notices are in the package, next to it. Source, issues and the changelog: github.com/donislawdev/BeanNetworkTester.

Metadata

Release files for beannetworktester 1.1.0

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

Built distributions (wheels)

Table of built distributions (wheels) for beannetworktester 1.1.0
File Interpreter ABI Platform Actions
beannetworktester-1.1.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for beannetworktester-1.1.0-py3-none-win_amd64.whl
beannetworktester-1.1.0-py3-none-manylinux_2_39_x86_64.whl Python 3 none Linux glibc 2.39+ x86-64 Details for beannetworktester-1.1.0-py3-none-manylinux_2_39_x86_64.whl
beannetworktester-1.1.0-py3-none-any.whl Python 3 none any Details for beannetworktester-1.1.0-py3-none-any.whl

Total release size: 29.3 MB

Release files / beannetworktester-1.1.0-py3-none-win_amd64.whl

Download URL beannetworktester-1.1.0-py3-none-win_amd64.whl
Size 16.7 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
2094e25ba378ec98a2f9ac7b522390222b2e25cd7777ead510545cbb386f93d3
BLAKE2b-256 checksum
How to use checksums
53a7bb2e26946621fc257c22aeae6b67e90e3e0c885bc9b6365345904373b7fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / beannetworktester-1.1.0-py3-none-manylinux_2_39_x86_64.whl

Download URL beannetworktester-1.1.0-py3-none-manylinux_2_39_x86_64.whl
Size 12.6 MB
Tags Linux glibc 2.39+ x86-64 Python 3
SHA-256 checksum
How to use checksums
c4523350ee97d28b2c1a2a594977f328db707f0f414cb595d55bb064397ce3ee
BLAKE2b-256 checksum
How to use checksums
935465dd8aca77eb80b5afa281c1cb28292c5238707b2aeb6676f7066207eac4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / beannetworktester-1.1.0-py3-none-any.whl

Download URL beannetworktester-1.1.0-py3-none-any.whl
Size 26.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f22ea638be8ff3497024e75614c631a579d037f23a8f4e6448039624f3a0ac3c
BLAKE2b-256 checksum
How to use checksums
de9cfca39a60ae0b273508d4f74049ab9b7f04950746477bc15a99363af7757d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for beannetworktester 1.1.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.

Full provenance

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for beannetworktester-1.1.0-py3-none-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

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

1.1.0 This release

3 release files Full 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