Skip to content

Commit 3b53ad5

Browse files
committed
Add /xunit.analyzers/rules/xUnit1053
1 parent ab6fa7d commit 3b53ad5

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
analyzer: true
3+
title: xUnit1053
4+
description: The static member used as theory data must be statically initialized
5+
category: Usage
6+
severity: Warning
7+
v2: true
8+
v3: true
9+
---
10+
11+
## Cause
12+
13+
A violation of this rule occurs when a member referenced by `[MemberData]` is not statically initialized.
14+
15+
## Reason for rule
16+
17+
Data provided by a member data source must be statically initialized so that it can be used to provide data for the tests that reference it with `[MemberData]`. Failing to initialize the data source will result in no data rows.
18+
19+
## How to fix violations
20+
21+
To fix a violation of this rule, either initialize the value inline or via a static constructor.
22+
23+
## Examples
24+
25+
### Violates
26+
27+
```csharp
28+
using Xunit;
29+
30+
public class xUnit1053
31+
{
32+
public static TheoryData<int> DataSource;
33+
34+
[Theory]
35+
[MemberData(nameof(DataSource))]
36+
public void TestMethod(int x) =>
37+
Assert.NotEqual(0, x);
38+
}
39+
```
40+
41+
### Does not violate
42+
43+
```csharp
44+
using Xunit;
45+
46+
public class xUnit1053
47+
{
48+
public static TheoryData<int> DataSource = [4, 9, 16];
49+
50+
[Theory]
51+
[MemberData(nameof(DataSource))]
52+
public void TestMethod(int x) =>
53+
Assert.NotEqual(0, x);
54+
}
55+
```
56+
57+
```csharp
58+
using Xunit;
59+
60+
public class xUnit1053
61+
{
62+
public static TheoryData<int> DataSource;
63+
64+
static xUnit1053() =>
65+
DataSource = [4, 9, 16];
66+
67+
[Theory]
68+
[MemberData(nameof(DataSource))]
69+
public void TestMethod(int x) =>
70+
Assert.NotEqual(0, x);
71+
}
72+
```

0 commit comments

Comments
 (0)