Best Python code snippet using slash
logbook_tests.py
Source:logbook_tests.py  
...9def logbook_handler(elasticapm_client):10    elasticapm_client.config.include_paths = ["tests", "elasticapm"]11    return LogbookHandler(elasticapm_client)12def test_logbook_logger_error_level(logbook_logger, logbook_handler):13    with logbook_handler.applicationbound():14        logbook_logger.error("This is a test error")15    assert len(logbook_handler.client.events) == 116    event = logbook_handler.client.events.pop(0)["errors"][0]17    assert event["log"]["logger_name"] == __name__18    assert event["log"]["level"] == "error"19    assert event["log"]["message"] == "This is a test error"20    assert "stacktrace" in event["log"]21    assert "exception" not in event22    assert "param_message" in event["log"]23    assert event["log"]["param_message"] == "This is a test error"24def test_logger_warning_level(logbook_logger, logbook_handler):25    with logbook_handler.applicationbound():26        logbook_logger.warning("This is a test warning")27    assert len(logbook_handler.client.events) == 128    event = logbook_handler.client.events.pop(0)["errors"][0]29    assert event["log"]["logger_name"] == __name__30    assert event["log"]["level"] == "warning"31    assert event["log"]["message"] == "This is a test warning"32    assert "stacktrace" in event["log"]33    assert "exception" not in event34    assert "param_message" in event["log"]35    assert event["log"]["param_message"] == "This is a test warning"36def test_logger_without_stacktrace_config(logbook_logger, logbook_handler):37    logbook_handler.client.config.auto_log_stacks = False38    with logbook_handler.applicationbound():39        logbook_logger.warning("This is a test warning")40    event = logbook_handler.client.events.pop(0)["errors"][0]41    assert "stacktrace" not in event["log"]42def test_logger_without_stacktrace_stack_false(logbook_logger, logbook_handler):43    logbook_handler.client.config.auto_log_stacks = True44    with logbook_handler.applicationbound():45        logbook_logger.warning("This is a test warning", stack=False)46    event = logbook_handler.client.events.pop(0)["errors"][0]47    assert "stacktrace" not in event["log"]48def test_logger_with_extra(logbook_logger, logbook_handler):49    with logbook_handler.applicationbound():50        logbook_logger.info("This is a test info with a url", extra=dict(url="http://example.com"))51    assert len(logbook_handler.client.events) == 152    event = logbook_handler.client.events.pop(0)["errors"][0]53    assert event["context"]["custom"]["url"] == "http://example.com"54    assert "stacktrace" in event["log"]55    assert "exception" not in event56    assert "param_message" in event["log"]57    assert event["log"]["param_message"] == "This is a test info with a url"58def test_logger_with_exc_info(logbook_logger, logbook_handler):59    with logbook_handler.applicationbound():60        try:61            raise ValueError("This is a test ValueError")62        except ValueError:63            logbook_logger.info("This is a test info with an exception", exc_info=True)64    assert len(logbook_handler.client.events) == 165    event = logbook_handler.client.events.pop(0)["errors"][0]66    assert event["log"]["message"] == "This is a test info with an exception"67    assert "stacktrace" in event["log"]68    assert "exception" in event69    exc = event["exception"]70    assert exc["type"] == "ValueError"71    assert exc["message"] == "ValueError: This is a test ValueError"72    assert "param_message" in event["log"]73    assert event["log"]["param_message"] == "This is a test info with an exception"74def test_logger_param_message(logbook_logger, logbook_handler):75    with logbook_handler.applicationbound():76        logbook_logger.info("This is a test of %s", "args")77    assert len(logbook_handler.client.events) == 178    event = logbook_handler.client.events.pop(0)["errors"][0]79    assert event["log"]["message"] == "This is a test of args"80    assert "stacktrace" in event["log"]81    assert "exception" not in event82    assert "param_message" in event["log"]83    assert event["log"]["param_message"] == "This is a test of %s"84def test_client_arg(elasticapm_client):85    handler = LogbookHandler(elasticapm_client)86    assert handler.client == elasticapm_client87def test_client_kwarg(elasticapm_client):88    handler = LogbookHandler(client=elasticapm_client)89    assert handler.client == elasticapm_client90def test_invalid_first_arg_type():91    with pytest.raises(ValueError):92        LogbookHandler(object)93def test_missing_client_arg():94    with pytest.raises(TypeError):95        LogbookHandler()96def test_logbook_handler_emit_error(capsys, elasticapm_client):97    handler = LogbookHandler(elasticapm_client)98    handler._emit = lambda: 1 / 099    handler.emit(LogRecord("x", 1, "Oops"))100    out, err = capsys.readouterr()101    assert "Top level ElasticAPM exception caught" in err102    assert "Oops" in err103def test_logbook_handler_dont_emit_elasticapm(capsys, elasticapm_client):104    handler = LogbookHandler(elasticapm_client)105    handler.emit(LogRecord("elasticapm.errors", 1, "Oops"))106    out, err = capsys.readouterr()107    assert "Oops" in err108def test_arbitrary_object(elasticapm_client, logbook_logger, logbook_handler):109    with logbook_handler.applicationbound():110        logbook_logger.info(["a", "list", "of", "strings"])111    assert len(logbook_handler.client.events) == 1112    event = logbook_handler.client.events.pop(0)["errors"][0]113    assert "param_message" in event["log"]...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!!
