2 using System
.Reflection
;
3 using System
.Collections
.Generic
;
5 [AttributeUsageAttribute(AttributeTargets
.All
, Inherited
= true, AllowMultiple
= true)]
6 public class CategoryAttribute
: Attribute
8 public CategoryAttribute (string category
) {
12 public string Category
{
16 public class TestDriverReporter
18 public int FailedTests { get; private set; }
19 public int SkippedTests { get; private set; }
20 public int ExecutedTests { get; private set; }
22 public void ReportResults (int executed
, int skipped
, int failed
) {
23 ExecutedTests
= executed
;
24 SkippedTests
= skipped
;
29 public class TestDriver
{
31 static public int RunTests(Type type
, string[] args
, TestDriverReporter reporter
) {
32 int failed
= 0, ran
= 0;
37 bool do_timings
= false;
41 DateTime start
, end
= DateTime
.Now
;
45 var exclude
= new Dictionary
<string, string> ();
46 List
<string> run_only
= new List
<string> ();
47 List
<string> exclude_test
= new List
<string> ();
48 if (args
!= null && args
.Length
> 0) {
49 for (j
= 0; j
< args
.Length
;) {
50 if (args
[j
] == "--time") {
53 } else if (args
[j
] == "--iter") {
54 iterations
= Int32
.Parse (args
[j
+ 1]);
56 } else if ((args
[j
] == "-v") || (args
[j
] == "--verbose")) {
59 } else if ((args
[j
] == "-q") || (args
[j
] == "--quiet")) {
64 } else if (args
[j
] == "--exclude") {
65 exclude
[args
[j
+ 1]] = args
[j
+ 1];
67 } else if (args
[j
] == "--exclude-test") {
68 exclude_test
.Add (args
[j
+ 1]);
70 } else if (args
[j
] == "--run-only") {
71 run_only
.Add (args
[j
+ 1]);
74 Console
.WriteLine ("Unknown argument: " + args
[j
]);
80 methods
= type
.GetMethods (BindingFlags
.Public
|BindingFlags
.NonPublic
|BindingFlags
.Static
);
81 for (int iter
= 0; iter
< iterations
; ++iter
) {
82 for (i
= 0; i
< methods
.Length
; ++i
) {
83 name
= methods
[i
].Name
;
84 if (!name
.StartsWith ("test_", StringComparison
.Ordinal
))
86 if (run_only
.Count
> 0) {
88 for (j
= 0; j
< run_only
.Count
; j
++) {
89 if (name
.EndsWith (run_only
[j
])) {
97 if (exclude
.Count
> 0 || exclude_test
.Count
> 0) {
98 var attrs
= methods
[i
].GetCustomAttributes (typeof (CategoryAttribute
), false);
100 for (j
= 0; j
< exclude_test
.Count
; j
++) {
101 if (name
.EndsWith (exclude_test
[j
])) {
106 foreach (CategoryAttribute attr
in attrs
) {
107 if (exclude
.ContainsKey (attr
.Category
))
112 Console
.WriteLine ("Skipping '{0}'.", name
);
117 for (j
= 5; j
< name
.Length
; ++j
)
118 if (!Char
.IsDigit (name
[j
]))
121 Console
.WriteLine ("Running '{0}' ...", name
);
122 expected
= Int32
.Parse (name
.Substring (5, j
- 5));
123 start
= DateTime
.Now
;
124 result
= (int)methods
[i
].Invoke (null, null);
127 long tdiff
= end
.Ticks
- start
.Ticks
;
128 int mdiff
= (int)tdiff
/10000;
130 Console
.WriteLine ("{0} took {1} ms", name
, mdiff
);
133 if (result
!= expected
) {
135 Console
.WriteLine ("{0} failed: got {1}, expected {2}", name
, result
, expected
);
141 Console
.WriteLine ("Total ms: {0}", tms
);
144 Console
.WriteLine ("Regression tests: {0} ran, {1} skipped, {2} failed in {3}", ran
, nskipped
, failed
, type
);
146 Console
.WriteLine ("Regression tests: {0} ran, {1} failed in {2}", ran
, failed
, type
);
150 if (reporter
!= null) {
151 reporter
.ReportResults (ran
, nskipped
, failed
);
154 //Console.WriteLine ("Regression tests: {0} ran, {1} failed in [{2}]{3}", ran, failed, type.Assembly.GetName().Name, type);
158 static public int RunTests (Type type
, string[] args
) {
159 return RunTests (type
, args
, null);
162 static public int RunTests (Type type
) {
163 return RunTests (type
, null, null);
167 /// Provide tests with the ability to find out how much time they have to run before being timed out.
168 public class TestTimeout
170 private TimeSpan Timeout { get; }
172 private DateTime StartTime { get; }
174 public bool HaveTimeLeft { get { return DateTime.UtcNow - StartTime < Timeout; }
}
176 public static bool IsStressTest { get { return Environment.GetEnvironmentVariable("MONO_TESTS_STRESS") == "1"; }
}
178 private TestTimeout (TimeSpan timeout
)
181 StartTime
= DateTime
.UtcNow
;
184 public static TestTimeout
Start(TimeSpan timeout
)
186 if (timeout
.Ticks
< 0)
188 throw new ArgumentException("timeout");
191 return new TestTimeout(timeout
);