2
// StressTestAttribute.cs
5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Reflection;
29
using System.Collections.Generic;
32
namespace MonoDevelop.Components.AutoTest
34
public class StressTest
36
List<TestCase> cachedTests;
42
public AutoTestClientSession Session {
43
get { return TestService.Session; }
46
protected virtual void Setup ()
50
protected virtual void TearDown ()
54
public void Run (TestPlan testPlan)
56
List<TestCase> tests = GetTests ();
59
for (int n=0; n<testPlan.Repeat; n++) {
60
Console.WriteLine ("Test suite iteration: " + n);
61
foreach (TestCase t in tests) {
62
int ti = testPlan.GetIterationsForTest (t.Method.Name);
63
for (int i=0; i<ti; i++) {
64
Console.WriteLine ("Running test {0}: {1}", t.Method.Name, i);
65
t.Method.Invoke (this, null);
66
if (i < ti-1 && t.UndoMethod != null) {
67
t.UndoMethod.Invoke (this, null);
75
public bool HasTest (string name)
77
return GetTests ().Any (t => t.Method.Name == name);
80
List<TestCase> GetTests ()
82
if (cachedTests != null)
85
List<TestCase> tests = new List<TestCase> ();
87
foreach (MethodInfo met in GetType().GetMethods ())
89
foreach (TestStepAttribute sat in met.GetCustomAttributes (typeof(TestStepAttribute), true)) {
90
TestCase tc = new TestCase () {
92
RunAfter = sat.RunAfter,
95
AddSorted (tests, tc);
97
foreach (UndoTestStepAttribute sat in met.GetCustomAttributes (typeof(UndoTestStepAttribute), true)) {
98
AddUndo (tests, sat.Undoes, met);
105
void AddSorted (List<TestCase> list, TestCase t)
107
for (int n=0; n<list.Count; n++) {
108
if (list[n].Method.Name == t.RunAfter) {
109
list.Insert (n+1, t);
112
if (list[n].RunAfter == t.Method.Name) {
120
void AddUndo (List<TestCase> list, string methodName, MethodInfo undoer)
122
foreach (TestCase t in list) {
123
if (t.Method.Name == methodName)
124
t.UndoMethod = undoer;
131
public MethodInfo Method { get; set; }
132
public MethodInfo UndoMethod { get; set; }
133
public string RunAfter { get; set; }
134
public string Stage { get; set; }
137
public class TestPlan
139
Dictionary<string,int> itersByTest = new Dictionary<string, int> ();
141
public int Repeat { get; set; }
143
public int Iterations { get; set; }
145
public int GetIterationsForTest (string name)
148
if (itersByTest.TryGetValue (name, out res))
151
return Iterations > 0 ? Iterations : 1;
154
public void SetIterationsForTest (string name, int iterations)
156
itersByTest [name] = iterations;