Added support in a unittest to avoid errors if using begin timer and run as a local unit test.
Showing
4 changed files
with
52 additions
and
30 deletions
TestProject1/LoadTest4.loadtest
0 → 100644
This diff is collapsed.
Click to expand it.
... | @@ -49,7 +49,9 @@ | ... | @@ -49,7 +49,9 @@ |
49 | <Reference Include="Microsoft.VisualStudio.QualityTools.LoadTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> | 49 | <Reference Include="Microsoft.VisualStudio.QualityTools.LoadTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> |
50 | <Private>False</Private> | 50 | <Private>False</Private> |
51 | </Reference> | 51 | </Reference> |
52 | <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> | 52 | <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
53 | <Private>False</Private> | ||
54 | </Reference> | ||
53 | <Reference Include="Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> | 55 | <Reference Include="Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> |
54 | <Reference Include="System" /> | 56 | <Reference Include="System" /> |
55 | <Reference Include="System.Core"> | 57 | <Reference Include="System.Core"> |
... | @@ -76,6 +78,9 @@ | ... | @@ -76,6 +78,9 @@ |
76 | <None Include="LoadTest3.loadtest"> | 78 | <None Include="LoadTest3.loadtest"> |
77 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> | 79 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> |
78 | </None> | 80 | </None> |
81 | <None Include="LoadTest4.loadtest"> | ||
82 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
83 | </None> | ||
79 | <None Include="WebTest22.webtest"> | 84 | <None Include="WebTest22.webtest"> |
80 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> | 85 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> |
81 | </None> | 86 | </None> |
... | @@ -85,10 +90,9 @@ | ... | @@ -85,10 +90,9 @@ |
85 | <None Include="WebTest3.webtest"> | 90 | <None Include="WebTest3.webtest"> |
86 | <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | 91 | <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |
87 | </None> | 92 | </None> |
88 | <Shadow Include="Test References\LIL_VSTT_Plugins.accessor" /> | 93 | <Resource Include="Userdata.csv"> |
89 | <None Include="Userdata.csv"> | 94 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> |
90 | <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | 95 | </Resource> |
91 | </None> | ||
92 | <None Include="WebTest1.webtest"> | 96 | <None Include="WebTest1.webtest"> |
93 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> | 97 | <CopyToOutputDirectory>Always</CopyToOutputDirectory> |
94 | </None> | 98 | </None> | ... | ... |
... | @@ -19,39 +19,37 @@ namespace TestProject1 | ... | @@ -19,39 +19,37 @@ namespace TestProject1 |
19 | // | 19 | // |
20 | } | 20 | } |
21 | 21 | ||
22 | private TestContext testContextInstance; | 22 | public TestContext TestContext { get; set; } |
23 | |||
24 | /// <summary> | ||
25 | ///Gets or sets the test context which provides | ||
26 | ///information about and functionality for the current test run. | ||
27 | ///</summary> | ||
28 | public TestContext TestContext | ||
29 | { | ||
30 | get | ||
31 | { | ||
32 | return testContextInstance; | ||
33 | } | ||
34 | set | ||
35 | { | ||
36 | testContextInstance = value; | ||
37 | } | ||
38 | } | ||
39 | 23 | ||
40 | #region Additional test attributes | 24 | #region Additional test attributes |
41 | // | 25 | // |
42 | // You can use the following additional attributes as you write your tests: | 26 | // You can use the following additional attributes as you write your tests: |
43 | // | 27 | // |
44 | // Use ClassInitialize to run code before running the first test in the class | 28 | // Use ClassInitialize to run code before running the first test in the class |
45 | // [ClassInitialize()] | 29 | |
46 | // public static void MyClassInitialize(TestContext testContext) { } | 30 | |
47 | // | 31 | // |
48 | // Use ClassCleanup to run code after all tests in a class have run | 32 | // Use ClassCleanup to run code after all tests in a class have run |
49 | // [ClassCleanup()] | 33 | // [ClassCleanup()] |
50 | // public static void MyClassCleanup() { } | 34 | // public static void MyClassCleanup() { } |
51 | // | 35 | // |
52 | // Use TestInitialize to run code before running each test | 36 | // Use TestInitialize to run code before running each test |
53 | // [TestInitialize()] | 37 | private bool inLoadTest = false; |
54 | // public void MyTestInitialize() { } | 38 | private bool dontKnow = true; |
39 | [TestInitialize()] | ||
40 | public void CheckTestContextType() { | ||
41 | if (dontKnow) | ||
42 | { | ||
43 | // Check if the TestContext type is the one that a loadtest provides | ||
44 | if (TestContext.GetType().ToString().Equals("Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext")) | ||
45 | inLoadTest = true; | ||
46 | else | ||
47 | inLoadTest = false; | ||
48 | dontKnow = false; | ||
49 | TestContext.WriteLine("We have a loadtest TestContext: " + inLoadTest); | ||
50 | } | ||
51 | } | ||
52 | |||
55 | // | 53 | // |
56 | // Use TestCleanup to run code after each test has run | 54 | // Use TestCleanup to run code after each test has run |
57 | // [TestCleanup()] | 55 | // [TestCleanup()] |
... | @@ -62,11 +60,33 @@ namespace TestProject1 | ... | @@ -62,11 +60,33 @@ namespace TestProject1 |
62 | [TestMethod] | 60 | [TestMethod] |
63 | public void TestMethod1() | 61 | public void TestMethod1() |
64 | { | 62 | { |
65 | TestContext.WriteLine("Current Test Context parameters:"); | 63 | // Parameters |
64 | int myThinkTime = 5; | ||
65 | |||
66 | TestContext.WriteLine("# Current Test Context parameters:"); | ||
66 | IDictionary<string, object> dic = (IDictionary<string, object>)TestContext.Properties; | 67 | IDictionary<string, object> dic = (IDictionary<string, object>)TestContext.Properties; |
67 | foreach (KeyValuePair<string, object> pair in dic) | 68 | foreach (KeyValuePair<string, object> pair in dic) |
68 | TestContext.WriteLine(pair.Key + ": " + pair.Value.ToString()); | 69 | TestContext.WriteLine(pair.Key + ": " + pair.Value.ToString()); |
69 | TestContext.WriteLine("End of Current Test Context parameters."); | 70 | TestContext.WriteLine("# End of Current Test Context parameters."); |
71 | |||
72 | // Get think time from load test provided test context if set | ||
73 | if (TestContext.Properties.Contains("ThinkTime")) | ||
74 | { | ||
75 | myThinkTime = int.Parse(TestContext.Properties["ThinkTime"].ToString()); | ||
76 | TestContext.WriteLine("Using thinktime " + myThinkTime + " from test context"); | ||
77 | } | ||
78 | |||
79 | // Dummy transaction | ||
80 | TestContext.WriteLine("Starting transaction UnitTestTransaction1"); | ||
81 | if (inLoadTest) TestContext.BeginTimer("UnitTestTransaction1"); | ||
82 | |||
83 | // Think time | ||
84 | System.Threading.Thread.Sleep(myThinkTime * 1000); | ||
85 | |||
86 | // End transaction | ||
87 | TestContext.WriteLine("Ending transaction UnitTestTransaction1"); | ||
88 | if (inLoadTest) TestContext.EndTimer("UnitTestTransaction1"); | ||
89 | |||
70 | } | 90 | } |
71 | } | 91 | } |
72 | } | 92 | } | ... | ... |
-
Please register or sign in to post a comment