Skip to content

Commit dfd495c

Browse files
committed
docs: add examples of good project structure and a small migration guide to avoiding global resources document
1 parent bd396fb commit dfd495c

File tree

1 file changed

+122
-37
lines changed

1 file changed

+122
-37
lines changed

docs/04_tip_and_tricks/avoiding_a_global_resource_file.md

Lines changed: 122 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,47 +69,132 @@ The recommended solution is to **modularize your resources**:
6969

7070
This modular approach not only eliminates issues like circular dependencies and performance bottlenecks but also enhances maintainability and clarity. It ensures that suite settings clearly document which components are required for each test suite.
7171

72+
### Example
73+
74+
A well-organized Robot Framework project might have a structure like this:
75+
76+
```
77+
project/
78+
├── lib/
79+
│ └───UserData.py
80+
├── resources/
81+
│ ├── api/
82+
│ │ ├── authentication.resource # API auth keywords
83+
│ │ ├── customers.resource # Customer API endpoints
84+
│ │ └── orders.resource # Order API endpoints
85+
│ ├── functional/
86+
│ │ ├── users.resource # Login page interactions
87+
│ │ ├── customers.resource # Customer page interactions
88+
│ ├── ui/
89+
│ │ ├── login.resource # Login page interactions
90+
│ │ ├── customers.resource # Customer page interactions
91+
│ │ └── common_elements.resource # Shared UI elements
92+
│ └── common/
93+
│ ├── test_data.resource # Test data generation
94+
│ └── utilities.resource # General helper keywords
95+
└── tests/
96+
├── api/
97+
│ └── customer_api_tests.robot # Imports only api/customers.resource
98+
├── api/
99+
└── business/
100+
└── contracts.robot # Imports only ui/login_page.resource
101+
└── ui_tests/
102+
└── login_tests.robot # Imports only ui/login_page.resource
103+
```
104+
105+
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)
106+
107+
Your settings section in a resource file for functional keywords, can be look like this:
108+
109+
```robot
110+
*** Settings ***
111+
# In login_tests.robot
112+
Resource ui/login.resource
113+
Resource ui/customers.resource
114+
Resource common/test_data.resource
115+
116+
```
117+
118+
and if you have a suite for functional tests, like this:
119+
120+
```robot
121+
*** Settings ***
122+
# In contracts.robot
123+
Resource functional/users.resource
124+
Resource functional/customers.resource
125+
Resource common/test_data.resource
126+
127+
```
128+
129+
### Migration Guide: From Global to Modular Structure
130+
131+
If you have an existing project with a large global resource file, consider this incremental approach:
132+
133+
1. **Analyze usage patterns**:
134+
- Identify which keywords/variables are actually used in each test suite
135+
- Look for natural functional groupings (UI, API, data generation, etc.)
136+
137+
2. **Create specialized resource files**:
138+
- Start with one functional area (e.g., login functionality)
139+
- Extract relevant keywords into a new `login.resource` file
140+
- Maintain the original global file temporarily
141+
142+
3. **Gradual transition**:
143+
- Update one test suite at a time to use the specific resource
144+
- Keep the global import during transition for backward compatibility
145+
- Run tests after each change to verify functionality
146+
147+
4. **Progressive cleanup**:
148+
- Once all suites using specific functionality import the correct resource file
149+
- Remove those keywords from the global file
150+
- Eventually phase out the global file completely
151+
152+
For keywords that are genuinely used everywhere, consider keeping a minimal `common.resource` file, but ensure it only contains truly global utilities.
153+
154+
72155
## When Restructuring Isn’t Possible
73156

74157
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.
75158

76-
- **Suppress Warnings in Specific Files:**
77-
Use directives to disable warnings for circular dependencies and already-imported resources on a per-file basis.
78-
79-
```robot
80-
# robotcode: ignore[*ResourceAlreadyImported*, PossibleCircularImport]
81-
*** Settings ***
82-
Variables variables
83-
84-
Resource already_imported.resource # robotcode: ignore[ResourceAlreadyImported]
85-
Resource circular_import.resource # robotcode: ignore[PossibleCircularImport]
86-
```
87-
88-
- **Suppress Warnings Globally:**
89-
For global suppression in VS Code, add the following to your `settings.json`:
90-
91-
```json
92-
"robotcode.analysis.diagnosticModifiers.ignore": [
93-
"PossibleCircularImport",
94-
"CircularImport",
95-
"ResourceAlreadyImported",
96-
"VariablesAlreadyImported",
97-
"LibraryAlreadyImported"
98-
]
99-
```
100-
101-
Alternatively, to remain IDE-independent, use a [`robot.toml`](/03_reference/config) file with these contents:
102-
103-
```toml
104-
[tool.robotcode-analyze.modifiers]
105-
ignore = [
106-
"PossibleCircularImport",
107-
"CircularImport",
108-
"ResourceAlreadyImported",
109-
"VariablesAlreadyImported",
110-
"LibraryAlreadyImported"
111-
]
112-
```
159+
### Suppress Warnings in Specific Files
160+
161+
Use directives to disable warnings for circular dependencies and already-imported resources on a per-file basis.
162+
163+
```robot
164+
# robotcode: ignore[*ResourceAlreadyImported*, PossibleCircularImport]
165+
*** Settings ***
166+
Variables variables
167+
168+
Resource already_imported.resource # robotcode: ignore[ResourceAlreadyImported]
169+
Resource circular_import.resource # robotcode: ignore[PossibleCircularImport]
170+
```
171+
172+
### Suppress Warnings Globally
173+
174+
For global suppression in VS Code, add the following to your `settings.json`:
175+
176+
```json
177+
"robotcode.analysis.diagnosticModifiers.ignore": [
178+
"PossibleCircularImport",
179+
"CircularImport",
180+
"ResourceAlreadyImported",
181+
"VariablesAlreadyImported",
182+
"LibraryAlreadyImported"
183+
]
184+
```
185+
186+
Alternatively, to remain IDE-independent, use a [`robot.toml`](/03_reference/config) file with these contents:
187+
188+
```toml
189+
[tool.robotcode-analyze.modifiers]
190+
ignore = [
191+
"PossibleCircularImport",
192+
"CircularImport",
193+
"ResourceAlreadyImported",
194+
"VariablesAlreadyImported",
195+
"LibraryAlreadyImported"
196+
]
197+
```
113198

114199
## Conclusion
115200

0 commit comments

Comments
 (0)