Skip to content

Commit 41232aa

Browse files
committed
Add examples for using CollectionFixture in xUnit tests
1 parent 7b9f1b2 commit 41232aa

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

docs/UnitTest/5 Fixtures.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,53 @@ public class PointFixture
7979
}
8080
public List<(Point2D, Point2D, double)> Points { get; set; }
8181

82+
}
83+
```
84+
85+
## Using CollectionFixture
86+
- `CollectionFixture` allows sharing context across multiple test classes.
87+
- Define a collection fixture class and use `CollectionDefinition` attribute.
88+
89+
```csharp title="Using CollectionFixture"
90+
[Collection("Point2D Collection")]
91+
public class Point2DTest
92+
{
93+
public Point2DTest(PointFixture pointFixture)
94+
{
95+
_Points = pointFixture.Points;
96+
}
97+
private List<(Point2D, Point2D, double)> _Points { get; }
98+
99+
[Fact]
100+
public void TestDistanceTo_WithMemberData()
101+
{
102+
foreach (var (pointA, pointB, expectedDistance) in _Points)
103+
{
104+
//Act
105+
var distance = pointA.DistanceTo(pointB);
106+
//Assert
107+
Assert.Equal(expectedDistance, distance,3);
108+
}
109+
}
110+
}
111+
112+
public class PointFixture
113+
{
114+
public PointFixture()
115+
{
116+
Points = new List<(Point2D, Point2D, double)>
117+
{
118+
(new Point2D(0,0), new Point2D(10,0), 10),
119+
(new Point2D(0,0), new Point2D(10,10), 14.1421),
120+
(new Point2D(1,2), new Point2D(3,4), 2.8284)
121+
};
122+
}
123+
public List<(Point2D, Point2D, double)> Points { get; set; }
124+
125+
}
126+
127+
[CollectionDefinition("Point2D Collection")]
128+
public class Point2DTestCollection : ICollectionFixture<PointFixture>
129+
{
82130
}
83131
```

0 commit comments

Comments
 (0)