Skip to content

Commit 87bd175

Browse files
committed
doc: update article about avoiding global resource files
1 parent dfd495c commit 87bd175

File tree

1 file changed

+113
-17
lines changed

1 file changed

+113
-17
lines changed

docs/04_tip_and_tricks/avoiding_a_global_resource_file.md

Lines changed: 113 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Avoiding a Global Resource File
22

3-
In many Robot Framework projects, there is a temptation to simplify setup by consolidating all resources and libraries into one global file—often named something like `Keywords.resource`—which is then usually the only resource imported into suites and other resource files. At first glance, this strategy appears to streamline the project structure by reducing the number of explicit imports and centralizing common functionality. However, while this approach may save time during initial configuration, it masks underlying design issues and introduces several long-term challenges. These challenges range from complex circular dependencies and naming collisions to performance slowdowns and decreased maintainability, which ultimately hinder the scalability and robustness of the test suite.
3+
## Introduction
4+
5+
In many Robot Framework projects, there is a temptation to simplify setup by consolidating all resources and libraries into one global file—often named something like `Keywords.resource`—which is then usually the only resource imported into suites and other resource files.
6+
7+
**Executive Summary**: While global resource files might seem convenient initially, they lead to circular dependencies, keyword ambiguities, performance issues, and maintenance challenges. This guide explains the problems of this approach and offers a modular alternative that improves code organization, maintainability, and performance.
48

59
## Why This Approach is Problematic
610

@@ -19,9 +23,38 @@ In many Robot Framework projects, there is a temptation to simplify setup by con
1923
- **Creation of Unnecessary References:**
2024
Relying on a centralized file forces all suites and resource files to reference the same global file, even if they only need a subset of its contents. This means that every suite is indirectly coupled to every keyword and variable in that file, regardless of whether they are used. Such an arrangement makes it difficult to determine which keywords are actually needed for a given suite. When updates or refactoring are required, developers may unintentionally modify or remove elements that other parts of the project still depend on. This extra layer of indirection complicates resource tracking, increases the likelihood of errors during maintenance, and can create confusion in larger teams where multiple developers modify the global file simultaneously.
2125

26+
### Example of Problematic Global Resource File
27+
28+
Consider this example of a typical global resource file that causes issues:
29+
30+
```robot
31+
*** Settings ***
32+
# Global.resource - tries to do everything
33+
Library SeleniumLibrary
34+
Library RequestsLibrary
35+
Library DatabaseLibrary
36+
Library OperatingSystem
37+
Resource LoginKeywords.resource
38+
Resource CustomerKeywords.resource
39+
Resource OrderKeywords.resource
40+
Resource ApiKeywords.resource
41+
Resource ReportingKeywords.resource
42+
# ...and 20+ more imports
43+
44+
*** Variables ***
45+
${GLOBAL_URL} https://example.com
46+
${DB_CONNECTION} connection_string
47+
# ...hundreds of variables for different modules
48+
49+
*** Keywords ***
50+
# 500+ keywords covering every aspect of the system
51+
```
52+
53+
When this file gets imported into multiple other resources and suite files, it creates a tangled web of dependencies.
54+
2255
## Documenting with Suite Settings
2356

24-
Declaring libraries and resources in the suite settings is not only a configuration step—it also serves as essential documentation. Test suites and test cases are more than just executable code; they document which functional areas of the application are under test. By explicitly declaring the required libraries and resources (e.g., for login processes, customer management, or database validations), you provide clear insight into the suite’s focus.
57+
Suite settings do more than just configure your test environment—they serve as essential documentation for your project. When you explicitly declare libraries, resources and also variables in your settings section, you're creating a clear record of which technical or functional areas of the application are being used in this file. This transparency helps team members, stakeholders, and testers quickly understand the suite's purpose.
2558

2659
For example:
2760

@@ -32,7 +65,7 @@ Resource CustomerManagement.resource
3265
Resource DatabaseValidation.resource
3366
```
3467

35-
This explicit declaration improves maintainability and helps new team members, stakeholders, and automated systems quickly understand the application areas being validated.
68+
This declaration immediately communicates that this suite deals with login processes, customer management, and database validation—improving maintainability and knowledge transfer within your team.
3669

3770
## Limitations in Import and Package Management
3871

@@ -95,9 +128,8 @@ project/
95128
└── tests/
96129
├── api/
97130
│ └── customer_api_tests.robot # Imports only api/customers.resource
98-
├── api/
99-
└── business/
100-
└── contracts.robot # Imports only ui/login_page.resource
131+
├── business/
132+
│ └── contracts.robot # Imports only ui/login_page.resource
101133
└── ui_tests/
102134
└── login_tests.robot # Imports only ui/login_page.resource
103135
```
@@ -149,32 +181,97 @@ If you have an existing project with a large global resource file, consider this
149181
- Remove those keywords from the global file
150182
- Eventually phase out the global file completely
151183

152-
For keywords that are genuinely used everywhere, consider keeping a minimal `common.resource` file, but ensure it only contains truly global utilities.
184+
Here's a concrete example of refactoring from a global approach to a modular one:
185+
186+
**Before (Global.resource):**
187+
```robot
188+
*** Settings ***
189+
Library SeleniumLibrary
190+
Library RequestsLibrary
191+
192+
*** Keywords ***
193+
Login To Application
194+
[Arguments] ${username} ${password}
195+
Open Browser ${URL} ${BROWSER}
196+
Input Text id=username ${username}
197+
Input Password id=password ${password}
198+
Click Button id=login-button
199+
200+
Get Customer Details
201+
[Arguments] ${customer_id}
202+
${response}= GET ${API_URL}/customers/${customer_id}
203+
[Return] ${response.json()}
204+
```
205+
206+
**After:**
207+
208+
**/resources/ui/login.resource:**
209+
```robot
210+
*** Settings ***
211+
Library SeleniumLibrary
212+
213+
*** Keywords ***
214+
Login To Application
215+
[Arguments] ${username} ${password}
216+
Open Browser ${URL} ${BROWSER}
217+
Input Text id=username ${username}
218+
Input Password id=password ${password}
219+
Click Button id=login-button
220+
```
221+
222+
**/resources/api/customers.resource:**
223+
```robot
224+
*** Settings ***
225+
Library RequestsLibrary
226+
227+
*** Keywords ***
228+
Get Customer Details
229+
[Arguments] ${customer_id}
230+
${response}= GET ${API_URL}/customers/${customer_id}
231+
[Return] ${response.json()}
232+
```
233+
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+
```
244+
245+
## When Restructuring Isn't Possible
153246

247+
If restructuring your project isn't an option, you can mitigate potential issues by managing warnings from your development environment. For example, you can suppress warnings related to circular dependencies and redundant imports on a per-file basis or globally.
154248

155-
## When Restructuring Isn’t Possible
249+
It's important to understand that suppressing warnings doesn't fix the underlying issues—it merely acknowledges them. When you choose to ignore specific diagnostics, you're making a conscious decision that these particular issues are acceptable in your codebase. This explicit acknowledgment is different from simply ignoring random warnings in your IDE. By documenting suppressions in your code or configuration files, you're communicating to your team that you understand the potential problem but have determined that it's an acceptable compromise given your project's constraints.
156250

157-
If restructuring your project isn’t an option, you can mitigate potential issues by managing warnings from your development environment. For example, you can suppress warnings related to circular dependencies and redundant imports on a per-file basis or globally.
251+
This approach is particularly useful during transitional periods or when working with legacy codebases where perfect architectural solutions aren't immediately feasible. However, these suppressions should ideally be revisited periodically to determine if the underlying issues can eventually be resolved through refactoring.
158252

159253
### Suppress Warnings in Specific Files
160254

161255
Use directives to disable warnings for circular dependencies and already-imported resources on a per-file basis.
162256

163257
```robot
164-
# robotcode: ignore[*ResourceAlreadyImported*, PossibleCircularImport]
258+
# example for disabling of specific messages for the whole file
259+
# robotcode: ignore[ResourceAlreadyImported, PossibleCircularImport]
165260
*** Settings ***
166261
Variables variables
167262
263+
# example for disabling of specific messages for a statement
168264
Resource already_imported.resource # robotcode: ignore[ResourceAlreadyImported]
169265
Resource circular_import.resource # robotcode: ignore[PossibleCircularImport]
170266
```
171267

172268
### Suppress Warnings Globally
173269

174-
For global suppression in VS Code, add the following to your `settings.json`:
270+
The preferred approach to suppress warnings globally is using a `robot.toml` configuration file. This method is IDE-independent and can be checked into version control to share with your team. Create a [`robot.toml`](/03_reference/config) file in your project root with these contents:
175271

176-
```json
177-
"robotcode.analysis.diagnosticModifiers.ignore": [
272+
```toml
273+
[tool.robotcode-analyze.modifiers]
274+
ignore = [
178275
"PossibleCircularImport",
179276
"CircularImport",
180277
"ResourceAlreadyImported",
@@ -183,11 +280,10 @@ For global suppression in VS Code, add the following to your `settings.json`:
183280
]
184281
```
185282

186-
Alternatively, to remain IDE-independent, use a [`robot.toml`](/03_reference/config) file with these contents:
283+
Alternatively, if you're working exclusively in VS Code, you can add the following to your `settings.json`, though this approach is not recommended for team environments as it only affects your local setup:
187284

188-
```toml
189-
[tool.robotcode-analyze.modifiers]
190-
ignore = [
285+
```json
286+
"robotcode.analysis.diagnosticModifiers.ignore": [
191287
"PossibleCircularImport",
192288
"CircularImport",
193289
"ResourceAlreadyImported",

0 commit comments

Comments
 (0)