You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/04_tip_and_tricks/avoiding_a_global_resource_file.md
+69-35Lines changed: 69 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ In many Robot Framework projects, there is a temptation to simplify setup by con
17
17
-**Performance Issues:**
18
18
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.
19
19
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
+
20
22
-**Decreased Maintainability:**
21
23
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.
# 500+ keywords covering every aspect of the system
51
53
```
52
54
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
+
53
57
When this file gets imported into multiple other resources and suite files, it creates a tangled web of dependencies.
54
58
55
59
## Documenting with Suite Settings
@@ -109,62 +113,69 @@ A well-organized Robot Framework project might have a structure like this:
109
113
```
110
114
project/
111
115
├── lib/
112
-
│ └───UserData.py
116
+
│ └──UserData.py
113
117
├── resources/
114
118
│ ├── api/
115
119
│ │ ├── authentication.resource # API auth keywords
116
120
│ │ ├── customers.resource # Customer API endpoints
│ │ └── common_elements.resource # Shared UI elements
125
129
│ └── 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
128
132
└── tests/
129
133
├── api/
130
-
│ └── customer_api_tests.robot # Imports only api/customers.resource
134
+
│ └── customer_api_tests.robot # Imports only api/customers.resource
131
135
├── business/
132
-
│ └── contracts.robot # Imports only ui/login_page.resource
136
+
│ └── contracts.robot # Imports only ui/login.resource
133
137
└── ui_tests/
134
-
└── login_tests.robot # Imports only ui/login_page.resource
138
+
└── login_tests.robot # Imports only ui/login.resource
135
139
```
136
140
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.
138
142
139
-
Your settings section in a resource file for functional keywords, can be look like this:
143
+
Your suite settings can look like this:
140
144
141
-
```robot
145
+
::: code-group
146
+
147
+
```robot [login.robot]
142
148
*** Settings ***
143
-
# In login_tests.robot
144
149
Resource ui/login.resource
145
150
Resource ui/customers.resource
146
151
Resource common/test_data.resource
147
152
148
153
```
149
154
150
-
and if you have a suite for functional tests, like this:
155
+
:::
151
156
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]
153
162
*** Settings ***
154
-
# In contracts.robot
155
163
Resource functional/users.resource
156
164
Resource functional/customers.resource
157
165
Resource common/test_data.resource
158
166
159
167
```
160
168
169
+
:::
170
+
161
171
### Migration Guide: From Global to Modular Structure
162
172
163
173
If you have an existing project with a large global resource file, consider this incremental approach:
164
174
165
175
1.**Analyze usage patterns**:
166
176
- Identify which keywords/variables are actually used in each test suite
167
177
- 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.
168
179
169
180
2.**Create specialized resource files**:
170
181
- 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
184
195
Here's a concrete example of refactoring from a global approach to a modular one:
185
196
186
197
**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]
188
213
*** Settings ***
189
214
Library SeleniumLibrary
190
215
Library RequestsLibrary
216
+
Library Collections
217
+
Resource common/test_data.resource
218
+
Resource common/utility.resource
191
219
192
220
*** Keywords ***
193
221
Login To Application
@@ -200,15 +228,31 @@ Login To Application
200
228
Get Customer Details
201
229
[Arguments] ${customer_id}
202
230
${response}= GET ${API_URL}/customers/${customer_id}
0 commit comments