liveBook · Manning

Assertion

As explained in Build Financial Software with Generative AI (From Scratch)

An assert statement checks that a condition is true during program execution and signals an assertion failure when it is false.

An assertion is a direct check that a program result matches an expected condition. In tests, it turns an expectation into an executable guard: when the condition is false, the test reports a failure with useful evidence about what was received instead. section 2.4.3

Assertions make API tests check behavior rather than merely send requests. A request can complete while using the wrong HTTP method, redirecting unexpectedly, or returning an incorrect status. Checking the status code immediately exposes those problems and keeps later field checks meaningful. section 4.8 section 2.4.3

Check a parsed payment-file header

Parse a sample header, keep the expected fields in a dictionary, and assert that the parsed result equals that dictionary. Separating expected data from the assertion makes failures easier to inspect in an IDE. Add further assertions when individual fields or earlier API responses need separate checks. section 2.4.3 section 2.4.3

Use assertions at the point where each expectation becomes testable:

  • Assert a successful API response before reading its returned fields. section 2.4.3
  • Assert that a returned file list contains the requested filename, with a message showing the unexpected response. section 7.3.1
  • Assert totals from a response against the amounts specified by a scenario. section 7.3.1
  • Match expected status codes to each endpoint: a create request may correctly return 201 rather than 200. section 4.8
Listing 7.14 Then BDD code
@then(parsers.re('I should have a file that
➥ includes the file "(?P<filename>.*)"')) #1
def then_file_check(filename, api_response): #2
    assert any( 
        response["filename"] == filename for response  #3
            in api_response["response"]  #3
    ), (f"Expected {filename} in"  #3
➥f" {api_response['response']}")  #3
 #3
@then(parsers.parse('it should have a total credit #3
➥ amount of "{credit_amount}"')) #4
def then_total_credit(credit_amount, api_response): #5
    response = api_response["response"][0]
➥["credit_total"] #6
    assert credit_amount == response, f"Expected
➥ {credit_amount} in {response}" #7

This BDD implementation shows assertions validating that an API response includes a filename and has the expected credit total, while attaching messages for failures. section 7.3.1

A passing assertion is only as trustworthy as its expectation. Generated tests and generated parsing code can agree with each other while both are wrong, so inspect assumptions, sample data, and expected results rather than treating automated output as proof. section 2.4.3

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Demonstrates equality, membership, and HTTP-status assertions with diagnostic messages, without detailing assertion runtime internals.
Examples
Uses a parsed ACH file header, file-list and credit-total scenarios, and Locust requests to /files endpoints.
What it assumes
Introduces checks through concrete test code and assumes only that expected results and API responses can be compared.
What you can do after
Diagnose/debug with assertions by exposing incorrect parser output, response fields, methods, redirects, and status-code expectations.
How it connects
Connects assertions to pytest, BDD scenarios, HTTP status codes, API responses, Locust, and generative AI.

Also explained in

Compare how 15 books explain assertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.