• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

localstack / localstack / 16910279859

12 Aug 2025 01:24PM UTC coverage: 86.848% (+0.01%) from 86.837%
16910279859

push

github

web-flow
fix unit test reporting (#12996)

66740 of 76847 relevant lines covered (86.85%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.27
/localstack-core/localstack/testing/pytest/in_memory_localstack.py
1
"""Pytest plugin that spins up a single localstack instance in the current interpreter that is shared
2
across the current test session.
3

4
Use in your module as follows::
5

6
    pytest_plugins = "localstack.testing.pytest.in_memory_localstack"
7

8
    @pytest.hookimpl()
9
    def pytest_configure(config):
10
        config.option.start_localstack = True
11

12
You can explicitly disable starting localstack by setting ``TEST_SKIP_LOCALSTACK_START=1`` or
13
``TEST_TARGET=AWS_CLOUD``."""
14

15
import logging
1✔
16
import os
1✔
17
import threading
1✔
18

19
import pytest
1✔
20
from _pytest.config import PytestPluginManager
1✔
21
from _pytest.config.argparsing import Parser
1✔
22
from _pytest.main import Session
1✔
23

24
from localstack import config as localstack_config
1✔
25
from localstack.config import is_env_true
1✔
26
from localstack.constants import ENV_INTERNAL_TEST_RUN
1✔
27

28
LOG = logging.getLogger(__name__)
1✔
29
LOG.info("Pytest plugin for in-memory-localstack session loaded.")
1✔
30

31
if localstack_config.is_collect_metrics_mode():
1✔
32
    pytest_plugins = "localstack.testing.pytest.metric_collection"
1✔
33

34
_started = threading.Event()
1✔
35

36

37
def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager):
1✔
38
    parser.addoption(
1✔
39
        "--start-localstack",
40
        action="store_true",
41
        default=False,
42
    )
43

44

45
@pytest.hookimpl(tryfirst=True)
1✔
46
def pytest_runtestloop(session: Session):
1✔
47
    # avoid starting up localstack if we only collect the tests (-co / --collect-only)
48
    if session.config.option.collectonly:
1✔
49
        return
×
50

51
    if not session.config.option.start_localstack:
1✔
52
        return
1✔
53

54
    from localstack.testing.aws.util import is_aws_cloud
1✔
55

56
    if is_env_true("TEST_SKIP_LOCALSTACK_START"):
1✔
57
        LOG.info("TEST_SKIP_LOCALSTACK_START is set, not starting localstack")
×
58
        return
×
59

60
    if is_aws_cloud():
1✔
61
        if not is_env_true("TEST_FORCE_LOCALSTACK_START"):
×
62
            LOG.info("Test running against aws, not starting localstack")
×
63
            return
×
64
        LOG.info("TEST_FORCE_LOCALSTACK_START is set, a Localstack instance will be created.")
×
65

66
    from localstack.utils.common import safe_requests
1✔
67

68
    if is_aws_cloud():
1✔
69
        localstack_config.DEFAULT_DELAY = 5
×
70
        localstack_config.DEFAULT_MAX_ATTEMPTS = 60
×
71

72
    # configure
73
    os.environ[ENV_INTERNAL_TEST_RUN] = "1"
1✔
74
    localstack_config.INCLUDE_STACK_TRACES_IN_HTTP_RESPONSE = True
1✔
75

76
    safe_requests.verify_ssl = False
1✔
77

78
    from localstack.runtime import current
1✔
79

80
    _started.set()
1✔
81
    runtime = current.initialize_runtime()
1✔
82
    # start runtime asynchronously
83
    threading.Thread(target=runtime.run).start()
1✔
84

85
    # wait for runtime to be ready
86
    if not runtime.ready.wait(timeout=120):
1✔
87
        raise TimeoutError("gave up waiting for runtime to be ready")
×
88

89

90
@pytest.hookimpl(trylast=True)
1✔
91
def pytest_sessionfinish(session: Session):
1✔
92
    # last pytest lifecycle hook (before pytest exits)
93
    if not _started.is_set():
1✔
94
        return
1✔
95

96
    from localstack.runtime import get_current_runtime
1✔
97

98
    try:
1✔
99
        get_current_runtime()
1✔
100
    except ValueError:
×
101
        LOG.warning("Could not access the current runtime in a pytest sessionfinish hook.")
×
102
        return
×
103

104
    get_current_runtime().shutdown()
1✔
105
    LOG.info("waiting for runtime to stop")
1✔
106

107
    # wait for runtime to shut down
108
    if not get_current_runtime().stopped.wait(timeout=20):
1✔
109
        LOG.warning("gave up waiting for runtime to stop, returning anyway")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc