UnitTest1.cs
3.07 KB
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
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class UnitTest1
{
public UnitTest1()
{
//
// TODO: Add constructor logic here
//
}
public TestContext TestContext { get; set; }
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
private bool inLoadTest = false;
private bool dontKnow = true;
[TestInitialize()]
public void CheckTestContextType() {
if (dontKnow)
{
// Check if the TestContext type is the one that a loadtest provides
if (TestContext.GetType().ToString().Equals("Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext"))
inLoadTest = true;
else
inLoadTest = false;
dontKnow = false;
TestContext.WriteLine("We have a loadtest TestContext: " + inLoadTest);
}
}
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void TestMethod1()
{
// Parameters
int myThinkTime = 5;
TestContext.WriteLine("# Current Test Context parameters:");
IDictionary<string, object> dic = (IDictionary<string, object>)TestContext.Properties;
foreach (KeyValuePair<string, object> pair in dic)
TestContext.WriteLine(pair.Key + ": " + pair.Value.ToString());
TestContext.WriteLine("# End of Current Test Context parameters.");
// Get think time from load test provided test context if set
if (TestContext.Properties.Contains("ThinkTime"))
{
myThinkTime = int.Parse(TestContext.Properties["ThinkTime"].ToString());
TestContext.WriteLine("Using thinktime " + myThinkTime + " from test context");
}
// Dummy transaction
TestContext.WriteLine("Starting transaction UnitTestTransaction1");
if (inLoadTest) TestContext.BeginTimer("UnitTestTransaction1");
// Think time
System.Threading.Thread.Sleep(myThinkTime * 1000);
// End transaction
TestContext.WriteLine("Ending transaction UnitTestTransaction1");
if (inLoadTest) TestContext.EndTimer("UnitTestTransaction1");
}
}
}