Converting Multi-Level Excel Test Cases to XMind Mind Maps Using Python
This article demonstrates how to use Python, together with the openpyxl and xmind‑sdk libraries, to read a multi‑level Excel test case file, transform its hierarchical data into XMind topics, and generate a visual mind‑map representation of the test cases.
In software testing, managing and executing test cases is crucial, and while Excel is a common tool, large numbers of cases benefit from visual organization using mind maps; this article explains how to convert a multi‑level Excel test case file into an XMind mind‑map using Python.
1. Preparation Before converting, install the required libraries with:
pip install openpyxl xmind-sdk2. Reading Test Cases The example uses test_cases.xlsx containing a sheet named "Multiple Levels". The following Python code loads the workbook, defines a function to calculate indentation based on leading spaces, iterates through rows, extracts fields, and stores each row as a dictionary in a list:
from openpyxl import load_workbook
import re
# Open Excel file
workbook = load_workbook('test_cases.xlsx')
# Get worksheet
sheet = workbook['Multiple Levels']
# Determine indentation level from row data
def get_indentation(row_data):
result = re.search(r'^(\s*)(\S.*)', row_data, re.IGNORECASE)
if result:
return len(result.group(1)) / 4
else:
return 0
# List to store data
data = []
# Iterate rows
for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, values_only=True):
indentation = get_indentation(row[1])
step_index = 2
steps = []
while sheet.cell(row=row[0]+1, column=step_index).value:
if get_indentation(sheet.cell(row=row[0]+1, column=step_index).value) > indentation:
steps[-1] += '\n' + sheet.cell(row=row[0]+1, column=step_index).value.strip()
else:
steps.append(sheet.cell(row=row[0]+1, column=step_index).value.strip())
step_index += 1
row_dict = {
"繁忙度": row[0],
"问题描述": row[1].strip(),
"步骤": steps,
"预期结果": row[step_index].strip()
}
data.append(row_dict)
print(data)3. Generating the XMind File Create an XMind workbook, sheet, and root topic:
from xmind.core import workbook, sheet, topic
# Create XMind workbook
xmind_workbook = workbook.Workbook()
# Set XMind file name
xmind_file = 'test_cases.xmind'
# Add a sheet to the workbook
xmind_sheet = xmind_workbook.addSheet()
xmind_sheet.setTitle('TestCases')
# Add root topic
root_topic = xmind_sheet.getRootTopic()
root_topic.setTitle('TestCases')Then loop through each test case, adding it as a topic with its steps and expected result, setting appropriate markers and hierarchy levels:
# Loop through test cases and convert to XMind
for row in data:
t = topic.TopicElement(ownerWorkbook=xmind_workbook)
t.setTitle(row['问题描述'])
t.setMarkerIds([row['繁忙度']])
root_topic.addSubTopic(t)
for i, step in enumerate(row['步骤']):
step_topic = topic.TopicElement(ownerWorkbook=xmind_workbook)
step_topic.setTitle(step)
step_topic.setMarkerIds([row['繁忙度']])
step_level = len(step.split('\n')) if '\n' in step else 1
step_topic.setStructureClass(f"level-{step_level}")
t.addSubTopic(step_topic)
expected_topic = topic.TopicElement(ownerWorkbook=xmind_workbook)
expected_topic.setTitle(row['预期结果'])
expected_topic.setMarkerIds([row['繁忙度']])
expected_topic.setStructureClass('level-3')
t.addSubTopic(expected_topic)The setMarkerIds() method assigns priority markers, and setStructureClass() defines the visual level (from level‑1 to level‑6) for each topic.
4. Saving the XMind File Finally, save the workbook to a file:
# Save XMind workbook to file
xmind_workbook.save(xmind_file)5. Summary The article shows how to read hierarchical test case data from Excel with openpyxl , transform it into XMind topics using xmind-sdk , and produce a mind‑map that helps organize and visualize test cases effectively.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.