如何设置仅展示数据库表对比失败记录并将结果标记为失败?
Solution to Highlight Only Mismatched Record and Mark Test as Failed
Got it, let's break down how to achieve exactly what you need—only showing that 22nd mismatched record and marking the whole check as failed. Here's a tailored Robot Framework script that does just that, along with clear explanations:
Step-by-Step Robot Framework Script
*** Settings *** Library DatabaseLibrary Library Collections *** Test Cases *** Validate SealTest Security Table Consistency # Connect to target database using your custom connection string Connect To Database cx_Oracle ${DB_CONNECT_STRING} # Fetch sorted records from both tables to ensure correct row alignment ${queryResults1}= Query Select * from QA_USER.SealTest_Security_A Order by SECURITY_ID ${queryResults2}= Query Select * from QA_USER.SealTest_Security_B Order by SECURITY_ID # Initialize variable to track the first (and only, per your scenario) mismatched record ${mismatched_details}= Set Variable ${None} # Iterate through each record pair (0-based index for 22 total records) :FOR ${idx} IN RANGE 0 22 ${row_a}= Get From List ${queryResults1} ${idx} ${row_b}= Get From List ${queryResults2} ${idx} # Check if current row pair doesn't match Run Keyword If "${row_a}" != "${row_b}" ... Set Variable ${mismatched_details} Record ${idx+1} (SECURITY_ID: ${row_a[0]}) mismatch:\nTable A: ${row_a}\nTable B: ${row_b} # Exit loop early once we find the mismatch (optimization since you know only 22nd is bad) Run Keyword If ${mismatched_details} != ${None} Exit For Loop # Final validation: Fail the test with mismatch details if found, else log success Run Keyword If ${mismatched_details} != ${None} ... Fail Table comparison failed. Details: ${mismatched_details} ... ELSE ... Log All 21 records match successfully. No mismatches found. # Cleanup database connection Disconnect From Database
Key Explanations
- Library Dependencies: We add
Collectionsto handle list operations, since query results are returned as lists of row tuples. - Row Alignment: Sorting by
SECURITY_IDensures we're comparing the exact same records across both tables. - Targeted Comparison: Instead of using the generic
should be equal(which would show all records on failure), we loop row-by-row. This lets us capture only the mismatched record's details. - Early Exit: Since you know only the 22nd record is mismatched, we exit the loop immediately after finding it to save execution time.
- Clear Failure Output: The
Failkeyword marks the entire test as failed and prints a human-readable message with the mismatched record's position,SECURITY_ID, and content from both tables.
Important Notes
- If your table columns might be in different orders, replace
Select *with explicit column names (e.g.,Select SECURITY_ID, NAME, STATUS ...) to ensure consistent row structure for comparison. - Ensure
${DB_CONNECT_STRING}is properly defined in your Robot Framework variables (e.g., in a*** Variables ***section or external resource file) with the correct cx_Oracle connection format.
内容的提问来源于stack exchange,提问作者Seaal
相关产品推荐
相关产品推荐

