Best Python code snippet using slash
test_collector.py
Source:test_collector.py
1import inspect2import threading3from typing import Any4from typing import Callable5from typing import Dict6from uuid import uuid47import attr8import mock9import pytest10from ddtrace.debugging._probe.model import LineProbe11from ddtrace.debugging._snapshot.collector import SnapshotCollector12from ddtrace.debugging._snapshot.model import Snapshot13@attr.s14class MockProbe(object):15 probe_id = attr.ib(type=str)16 condition = attr.ib(type=Callable[[Dict[str, Any]], Any])17@attr.s18class MockFrame(object):19 f_locals = attr.ib(type=dict)20@attr.s21class MockThread(object):22 ident = attr.ib(type=int)23 name = attr.ib(type=str)24def mock_encoder(wraps=None):25 encoder = mock.Mock(wraps=wraps)26 snapshot_encoder = mock.Mock()27 encoder._encoders = {Snapshot: snapshot_encoder}28 return encoder, snapshot_encoder29def test_collector_cond():30 encoder, _ = mock_encoder()31 collector = SnapshotCollector(encoder=encoder)32 collector.push(33 MockProbe(uuid4(), lambda _: _["a"] is not None),34 MockFrame(dict(a=42)),35 MockThread(-1, "MainThread"),36 (Exception, Exception("foo"), None),37 )38 collector.push(39 MockProbe(uuid4(), lambda _: _["b"] is not None),40 MockFrame(dict(b=None)),41 MockThread(-2, "WorkerThread"),42 (Exception, Exception("foo"), None),43 )44 encoder.put.assert_called_once()45def test_collector_collect_enqueue():46 encoder, snapshot_encoder = mock_encoder()47 def wrapped(posarg, foo):48 i = posarg49 posarg = i << 150 return "[%d] %s" % (i, foo)51 collector = SnapshotCollector(encoder=encoder)52 for i in range(10):53 with collector.collect(54 LineProbe(probe_id="batch-test", source_file="foo.py", line=42),55 inspect.currentframe(),56 threading.current_thread(),57 [("posarg", i), ("foo", "bar")],58 ) as sc:59 assert sc.snapshot.entry_capture60 sc.return_value = wrapped(i, foo="bar")61 assert "[%d] bar" % i == sc.return_value62 assert sc.snapshot.return_capture63 assert len(snapshot_encoder.capture_context.mock_calls) == 2064 assert all(65 {"@return"} == {n for n, _ in call.args[1]} for call in snapshot_encoder.capture_context.mock_calls[1::2]66 ), [{n for n, _ in call.args[1]} for call in snapshot_encoder.capture_context.mock_calls[1::2]]67 assert len(encoder.put.mock_calls) == 1068def test_collector_collect_exception_enqueue():69 encoder, snapshot_encoder = mock_encoder()70 class MockException(Exception):71 pass72 def wrapped():73 raise MockException("test")74 collector = SnapshotCollector(encoder=encoder)75 for _ in range(10):76 with pytest.raises(MockException), collector.collect(77 LineProbe(probe_id="batch-test", source_file="foo.py", line=42),78 inspect.currentframe(),79 threading.current_thread(),80 [],81 ) as sc:82 assert sc.snapshot.entry_capture83 sc.return_value = wrapped()84 assert sc.snapshot.return_capture85 assert len(snapshot_encoder.capture_context.mock_calls) == 2086 assert all("@return" not in {n for n, _ in call.args[0]} for call in snapshot_encoder.capture_context.mock_calls)87 assert all(call.args[2][0] == MockException for call in snapshot_encoder.capture_context.mock_calls[1::2])88 assert len(encoder.put.mock_calls) == 1089def test_collector_push_enqueue():90 encoder, _ = mock_encoder()91 collector = SnapshotCollector(encoder=encoder)92 for _ in range(10):93 collector.push(94 MockProbe(uuid4(), None),95 inspect.currentframe(),96 threading.current_thread(),97 (Exception, Exception("foo"), None),98 )...
testing.py
Source:testing.py
...21 context_created.connect(self.capture_context, self.application)22 return self23 def __exit__(self, exc_type, exc_value, tb):24 context_created.disconnect(self.capture_context, self.application)25 def capture_context(self, app, context):26 self.captured_context = context27 @contextmanager28 def session(self, *args, **kwargs):29 if self.cookie_jar is None:30 raise RuntimeError('Session transactions only make sense '31 'with cookies enabled.')32 app = self.application33 environ_overrides = kwargs.setdefault('environ_overrides', {})34 self.cookie_jar.inject_wsgi(environ_overrides)35 with app.test_context(*args, **kwargs) as cx:36 sess = cx.session37 yield sess38 resp = app.response_class()39 app.save_session(cx, resp)...
test_capture_context.py
Source:test_capture_context.py
1import pytest2from formulaic.utils.context import capture_context3def test_capture_context():4 A = 15 def nested_context(context=0):6 A = 27 return capture_context(context)8 assert capture_context()["A"] == 19 assert nested_context()["A"] == 210 assert nested_context(1)["A"] == 111 with pytest.raises(ValueError, match="call stack is not deep enough"):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!