-
Notifications
You must be signed in to change notification settings - Fork 222
/
TestMethodShouldContainAssertion.Custom.cs
111 lines (91 loc) · 2.79 KB
/
TestMethodShouldContainAssertion.Custom.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CustomTests
{
using TestFramework;
using TestFramework.Attributes;
[TestClass]
public class BaseTest
{
[AssertionMethod]
protected virtual void CustomAssertionMethod() { }
[TestMethod]
public void TestMethod1() // Noncompliant {{Add at least one assertion to this test case.}}
// ^^^^^^^^^^^
{
var x = 42;
}
[TestMethod]
public void TestMethod2() // Compliant
{
Validator.StaticWithAttribute();
}
[TestMethod]
public void TestMethod3() // Noncompliant
{
Validator.StaticWrongAttribute();
}
[TestMethod]
public void TestMethod4() // Noncompliant
{
Validator.StaticNoAttribute();
}
[TestMethod]
public void TestMethod5() // Compliant
{
var validator = new Validator();
validator.InstanceWithAttribute();
}
[TestMethod]
public void TestMethod6() => // Compliant
new Validator().InstanceWithAttributeAndArg(null);
[TestMethod]
public void TestMethod7() => // Noncompliant, attribute must be on the method itself
AttributedType.AttributeOnType();
[TestMethod]
[DerivedExpectedException]
public void TestMethod8() // Compliant
{
var x = 42;
}
}
[TestClass]
public class DerivedTest : BaseTest
{
[TestMethod]
public void Derived() // Compliant
{
CustomAssertionMethod();
}
protected override void CustomAssertionMethod()
{
}
}
}
namespace TestFramework
{
using TestFramework.Attributes;
public class Validator
{
[AssertionMethodAttribute]
public static void StaticWithAttribute() { }
[NotAssertionMethodAttribute]
public static void StaticWrongAttribute() { }
public static void StaticNoAttribute() { }
[AssertionMethod]
public bool InstanceWithAttribute() => true;
[AssertionMethod]
public bool InstanceWithAttributeAndArg(object arg) => true;
}
[AssertionMethod] // Missused attribute
public static class AttributedType
{
public static void AttributeOnType() { } // Not an assertion method
}
}
namespace TestFramework.Attributes
{
public class AssertionMethodAttribute : Attribute { }
public class NotAssertionMethodAttribute : Attribute { } // AssertionMethodAttribute doesn't count as an assertion method attribute
public class DerivedExpectedExceptionAttribute : ExpectedExceptionBaseAttribute { protected override void Verify(Exception exception) { } }
}