|
| 1 | +# Test Quality Guidelines & Anti-Patterns |
| 2 | + |
| 3 | +## Test Quality Checklist |
| 4 | + |
| 5 | +- ✅ **One behavior per test** - Each test validates exactly one scenario |
| 6 | +- ✅ **Descriptive names** - Test name clearly describes the scenario |
| 7 | +- ✅ **Arrange-Act-Assert** - Clear separation of test phases |
| 8 | +- ✅ **Independent tests** - Tests don't depend on each other's state |
| 9 | +- ✅ **Proper cleanup** - Resources are disposed, temp files removed |
| 10 | +- ✅ **Meaningful assertions** - Assertions validate the important behavior |
| 11 | +- ✅ **No hardcoded paths** - Use TestDirectory, SampleDirectory, TempFolder |
| 12 | +- ✅ **Category tags** - Apply appropriate [Category] attributes |
| 13 | + |
| 14 | +## Common Anti-Patterns to Avoid |
| 15 | + |
| 16 | +### ❌ Multiple behaviors in one test |
| 17 | +```csharp |
| 18 | +// DON'T: Testing multiple unrelated things |
| 19 | +[Test] |
| 20 | +public void TestEverything() |
| 21 | +{ |
| 22 | + // Tests creation, execution, and saving all in one test |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### ✅ Split into focused tests |
| 27 | +```csharp |
| 28 | +[Test] public void NodeCreatedWithCorrectProperties() { } |
| 29 | +[Test] public void NodeExecutesAndProducesExpectedOutput() { } |
| 30 | +[Test] public void WorkspaceSavesWithCorrectStructure() { } |
| 31 | +``` |
| 32 | + |
| 33 | +### ❌ Ignored tests without explanation |
| 34 | +```csharp |
| 35 | +[Test] |
| 36 | +[Ignore] // DON'T: No explanation why |
| 37 | +public void SomeTest() { } |
| 38 | +``` |
| 39 | + |
| 40 | +### ✅ Documented ignored tests |
| 41 | +```csharp |
| 42 | +[Test] |
| 43 | +[Ignore("WIP - Node execution logic being refactored in DYN-1234")] |
| 44 | +public void NewNodeExecutesCorrectly() { } |
| 45 | +``` |
| 46 | + |
| 47 | +### ❌ Empty catch blocks |
| 48 | +```csharp |
| 49 | +try |
| 50 | +{ |
| 51 | + RiskyOperation(); |
| 52 | +} |
| 53 | +catch |
| 54 | +{ |
| 55 | + // DON'T: Silent failure |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### ✅ Proper error handling |
| 60 | +```csharp |
| 61 | +try |
| 62 | +{ |
| 63 | + RiskyOperation(); |
| 64 | +} |
| 65 | +catch (ExpectedException ex) |
| 66 | +{ |
| 67 | + // Log the error for debugging |
| 68 | + Console.WriteLine($"Expected error: {ex.Message}"); |
| 69 | + // Re-throw or handle appropriately |
| 70 | + throw; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### ❌ Weak assertions |
| 75 | +```csharp |
| 76 | +// DON'T: Weakened assertions |
| 77 | +Assert.IsNotNull(result); // When you could be more specific |
| 78 | +
|
| 79 | +// DO: Specific assertions |
| 80 | +Assert.AreEqual(expectedValue, result); |
| 81 | +Assert.AreEqual(3, collection.Count()); |
| 82 | +``` |
| 83 | + |
| 84 | +### ❌ Tests with side effects |
| 85 | +```csharp |
| 86 | +// DON'T: Tests affecting global state |
| 87 | +[Test] |
| 88 | +public void TestThatChangesGlobalConfig() |
| 89 | +{ |
| 90 | + GlobalConfig.Setting = "test value"; // Affects other tests |
| 91 | + // ... test logic |
| 92 | + // No cleanup |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### ✅ Isolated tests with cleanup |
| 97 | +```csharp |
| 98 | +[Test] |
| 99 | +public void TestWithProperIsolation() |
| 100 | +{ |
| 101 | + // Arrange |
| 102 | + var originalValue = GlobalConfig.Setting; |
| 103 | + |
| 104 | + try |
| 105 | + { |
| 106 | + // Act |
| 107 | + GlobalConfig.Setting = "test value"; |
| 108 | + // ... test logic |
| 109 | + } |
| 110 | + finally |
| 111 | + { |
| 112 | + // Cleanup |
| 113 | + GlobalConfig.Setting = originalValue; |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## File Organization Guidelines |
| 119 | + |
| 120 | +### Test File Structure |
| 121 | +``` |
| 122 | +test/ |
| 123 | +├── DynamoCoreTests/ # Core engine tests |
| 124 | +│ ├── Graph/ |
| 125 | +│ ├── Nodes/ |
| 126 | +│ └── Utilities/ |
| 127 | +├── DynamoCoreWpfTests/ # UI component tests |
| 128 | +├── Libraries/ # Per-library tests |
| 129 | +│ ├── CoreNodesTests/ |
| 130 | +│ └── PythonNodeModelsTests/ |
| 131 | +└── System/ # Integration tests |
| 132 | +``` |
| 133 | + |
| 134 | +### Test Categories |
| 135 | +- `[Category("UnitTests")]` - Unit tests (including focused .dyn file tests) |
| 136 | +- `[Category("RegressionTests")]` - Tests for specific bug fixes |
| 137 | +- `[Category("Failure")]` - Tests that demonstrate known failures |
| 138 | + |
| 139 | +**Note**: Dynamo uses a pragmatic approach where tests loading .dyn files can be categorized as unit tests when they focus on testing specific functionality rather than broad integration scenarios. |
| 140 | + |
| 141 | +## Naming Conventions |
| 142 | + |
| 143 | +### Preferred Naming Styles |
| 144 | + |
| 145 | +1. **Descriptive approach** (when scenario is clear): |
| 146 | + ```csharp |
| 147 | + HomeWorkspaceCanSaveAsNewFile() |
| 148 | + CustomNodeFunctionalityWorksCorrectly() |
| 149 | + ``` |
| 150 | + |
| 151 | +2. **When/Then format** (for complex scenarios): |
| 152 | + ```csharp |
| 153 | + WhenNodeIsCreatedThenItHasCorrectProperties() |
| 154 | + WhenInvalidInputThenThrowsException() |
| 155 | + ``` |
| 156 | + |
| 157 | +### Naming Guidelines |
| 158 | +- Use present tense for actions |
| 159 | +- Be specific about the scenario being tested |
| 160 | +- Include the expected outcome when it's not obvious |
| 161 | +- Avoid generic names like `Test1()` or `TestMethod()` |
0 commit comments