Skip to content

Commit 7800f67

Browse files
committed
docs: enhance guidance on modular resource file structure and performance considerations
1 parent e91918d commit 7800f67

File tree

1 file changed

+69
-35
lines changed

1 file changed

+69
-35
lines changed

docs/04_tip_and_tricks/avoiding_a_global_resource_file.md

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ In many Robot Framework projects, there is a temptation to simplify setup by con
1717
- **Performance Issues:**
1818
For every keyword call, Robot Framework iterates through the entire list of known keywords—and code analysis tools like RobotCode must do the same. The performance impact significantly depends on whether the system checks 50, 500, or even 5000 keywords; indeed, 500 to 5000 keywords is a realistic number in an average project. This process is especially time-consuming with embedded arguments that require regular expressions rather than simple string comparisons. A large global file filled with rarely used elements can significantly slow down keyword resolution, particularly in larger projects.
1919

20+
*Heuristic:* If a global resource file contains more than ~200 keywords or imports more than ~20 resource files, consider modularizing to improve performance and maintainability.
21+
2022
- **Decreased Maintainability:**
2123
While the number of resource files may be relatively small—perhaps 50 to 100—the combined global resource file can easily contain 500 or more keywords. This consolidation makes it difficult for test writers to quickly locate the relevant keyword or variable among hundreds of entries. The dense aggregation reduces clarity and increases the maintenance burden, as even a small change might affect multiple areas of the project. Refactoring becomes more challenging when the entire functionality is bundled into a single file, since updates or corrections must be made with care to avoid unintended side effects across the project.
2224

@@ -50,6 +52,8 @@ ${DB_CONNECTION} connection_string
5052
# 500+ keywords covering every aspect of the system
5153
```
5254

55+
Note: importing the same library or resource with different parameters in different places can lead to earlier instances being reused or overwritten, causing surprising behavior. Avoid parameterized duplicates and prefer small, purpose-specific imports.
56+
5357
When this file gets imported into multiple other resources and suite files, it creates a tangled web of dependencies.
5458

5559
## Documenting with Suite Settings
@@ -109,62 +113,69 @@ A well-organized Robot Framework project might have a structure like this:
109113
```
110114
project/
111115
├── lib/
112-
│ └──UserData.py
116+
│ └── UserData.py
113117
├── resources/
114118
│ ├── api/
115119
│ │ ├── authentication.resource # API auth keywords
116120
│ │ ├── customers.resource # Customer API endpoints
117-
│ │ └── orders.resource # Order API endpoints
121+
│ │ └── orders.resource # Order API endpoints
118122
│ ├── functional/
119-
│ │ ├── users.resource # Login page interactions
120-
│ │ ├── customers.resource # Customer page interactions
123+
│ │ ├── users.resource # User domain keywords
124+
│ │ ├── customers.resource # Customer domain keywords
121125
│ ├── ui/
122-
│ │ ├── login.resource # Login page interactions
123-
│ │ ├── customers.resource # Customer page interactions
124-
│ │ └── common_elements.resource # Shared UI elements
126+
│ │ ├── login.resource # Login page interactions
127+
│ │ ├── customers.resource # Customer page interactions
128+
│ │ └── common_elements.resource # Shared UI elements
125129
│ └── common/
126-
│ ├── test_data.resource # Test data generation
127-
│ └── utilities.resource # General helper keywords
130+
│ ├── test_data.resource # Test data generation
131+
│ └── utilities.resource # General helper keywords
128132
└── tests/
129133
├── api/
130-
│ └── customer_api_tests.robot # Imports only api/customers.resource
134+
│ └── customer_api_tests.robot # Imports only api/customers.resource
131135
├── business/
132-
│ └── contracts.robot # Imports only ui/login_page.resource
136+
│ └── contracts.robot # Imports only ui/login.resource
133137
└── ui_tests/
134-
└── login_tests.robot # Imports only ui/login_page.resource
138+
└── login_tests.robot # Imports only ui/login.resource
135139
```
136140

137-
In this structure, each test file imports only the specific resources it needs, avoiding a global import file. If you put the `resources` folder to your python path (this is the default for RobotCode)
141+
In this structure, each test file imports only the specific resources it needs, avoiding a global import file. Note: Robot Framework resolves Resource imports relative to the importing file (or via absolute paths). PYTHONPATH applies to Python libraries, not resource files. RobotCode can configure analysis and discovery paths via robot.toml.
138142

139-
Your settings section in a resource file for functional keywords, can be look like this:
143+
Your suite settings can look like this:
140144

141-
```robot
145+
::: code-group
146+
147+
```robot [login.robot]
142148
*** Settings ***
143-
# In login_tests.robot
144149
Resource ui/login.resource
145150
Resource ui/customers.resource
146151
Resource common/test_data.resource
147152
148153
```
149154

150-
and if you have a suite for functional tests, like this:
155+
:::
151156

152-
```robot
157+
and if you have a suite for functional tests, it can look like this:
158+
159+
::: code-group
160+
161+
```robot [contracts.robot]
153162
*** Settings ***
154-
# In contracts.robot
155163
Resource functional/users.resource
156164
Resource functional/customers.resource
157165
Resource common/test_data.resource
158166
159167
```
160168

169+
:::
170+
161171
### Migration Guide: From Global to Modular Structure
162172

163173
If you have an existing project with a large global resource file, consider this incremental approach:
164174

165175
1. **Analyze usage patterns**:
166176
- Identify which keywords/variables are actually used in each test suite
167177
- Look for natural functional groupings (UI, API, data generation, etc.)
178+
- Measure counts (keywords, variables, and resource imports) per file to prioritize refactoring; use a threshold (for example ~200 keywords) as a guide.
168179

169180
2. **Create specialized resource files**:
170181
- Start with one functional area (e.g., login functionality)
@@ -184,10 +195,27 @@ If you have an existing project with a large global resource file, consider this
184195
Here's a concrete example of refactoring from a global approach to a modular one:
185196

186197
**Before (Global.resource):**
187-
```robot
198+
199+
::: code-group
200+
201+
```robot [/tests/login_test.robot]
202+
*** Settings ***
203+
# Anti-pattern: single global import
204+
Resource resources/Global.resource
205+
206+
*** Test Cases ***
207+
Valid Login
208+
Login To Application valid_user valid_password
209+
Get Customer Details 123
210+
```
211+
212+
```robot [/resources/Global.resource]
188213
*** Settings ***
189214
Library SeleniumLibrary
190215
Library RequestsLibrary
216+
Library Collections
217+
Resource common/test_data.resource
218+
Resource common/utility.resource
191219
192220
*** Keywords ***
193221
Login To Application
@@ -200,15 +228,31 @@ Login To Application
200228
Get Customer Details
201229
[Arguments] ${customer_id}
202230
${response}= GET ${API_URL}/customers/${customer_id}
203-
[Return] ${response.json()}
231+
RETURN ${response.json()}
204232
```
205233

234+
:::
235+
206236
**After:**
207237

208-
**/resources/ui/login.resource:**
209-
```robot
238+
::: code-group
239+
240+
```robot [/tests/login_test.robot]
241+
*** Settings ***
242+
# Only importing what is needed
243+
Resource ui/login.resource
244+
Resource api/customers.resource
245+
246+
*** Test Cases ***
247+
Valid Login
248+
Login To Application valid_user valid_password
249+
Get Customer Details 123
250+
```
251+
252+
```robot [/resources/ui/login.resource]
210253
*** Settings ***
211254
Library SeleniumLibrary
255+
Resource common/utility.resource
212256
213257
*** Keywords ***
214258
Login To Application
@@ -219,8 +263,7 @@ Login To Application
219263
Click Button id=login-button
220264
```
221265

222-
**/resources/api/customers.resource:**
223-
```robot
266+
```robot [/resources/api/customers.resource]
224267
*** Settings ***
225268
Library RequestsLibrary
226269
@@ -231,16 +274,7 @@ Get Customer Details
231274
[Return] ${response.json()}
232275
```
233276

234-
**/tests/login_test.robot:**
235-
```robot
236-
*** Settings ***
237-
Resource resources/ui/login.resource
238-
# Only importing what is needed
239-
240-
*** Test Cases ***
241-
Valid Login
242-
Login To Application valid_user valid_password
243-
```
277+
:::
244278

245279
## When Restructuring Isn't Possible
246280

0 commit comments

Comments
 (0)