1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 namespace NUnit.Framework
33 using System.Collections.Generic;
34 using NUnit.Framework.Interfaces;
35 using NUnit.Framework.Internal;
36 using NUnit.Framework.Internal.Builders;
39 /// Adding this attribute to a method within a <seealso cref="TestFixtureAttribute"/>
40 /// class makes the method callable from the NUnit test runner. There is a property
41 /// called Description which is optional which you can provide a more detailed test
42 /// description. This class cannot be inherited.
47 /// public class Fixture
50 /// public void MethodToTest()
53 /// [Test(Description = "more detailed description")]
54 /// public void TestDescriptionMethod()
59 [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=true)]
60 public class TestAttribute : NUnitAttribute, ISimpleTestBuilder, IApplyToTest, IImplyFixture
62 private object _expectedResult;
63 private readonly NUnitTestCaseBuilder _builder = new NUnitTestCaseBuilder();
66 /// Descriptive text for this test
68 public string Description { get; set; }
71 /// The author of this test
73 public string Author { get; set; }
76 /// The type that this test is testing
78 public Type TestOf { get; set; }
80 #region IApplyToTest Members
83 /// Modifies a test by adding a description, if not already set.
85 /// <param name="test">The test to modify</param>
86 public void ApplyToTest(Test test)
88 if (!test.Properties.ContainsKey(PropertyNames.Description) && Description != null)
89 test.Properties.Set(PropertyNames.Description, Description);
91 if (!test.Properties.ContainsKey(PropertyNames.Author) && Author != null)
92 test.Properties.Set(PropertyNames.Author, Author);
94 if (!test.Properties.ContainsKey(PropertyNames.TestOf) && TestOf != null)
95 test.Properties.Set(PropertyNames.TestOf, TestOf.FullName);
101 #region ITestExpectedResult Members
104 /// Gets or sets the expected result.
106 /// <value>The result.</value>
107 public object ExpectedResult
109 get { return _expectedResult; }
112 _expectedResult = value;
113 HasExpectedResult = true;
118 /// Returns true if an expected result has been set
120 public bool HasExpectedResult { get; private set; }
124 #region ISimpleTestBuilder Members
127 /// Construct a TestMethod from a given method.
129 /// <param name="method">The method for which a test is to be constructed.</param>
130 /// <param name="suite">The suite to which the test will be added.</param>
131 /// <returns>A TestMethod</returns>
132 public TestMethod BuildFrom(IMethodInfo method, Test suite)
134 TestCaseParameters parms = null;
136 if (this.HasExpectedResult)
138 parms = new TestCaseParameters();
139 parms.ExpectedResult = this.ExpectedResult;
142 return _builder.BuildTestMethod(method, suite, parms);