[1146] | 1 | <!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
---|
| 2 | <html>
|
---|
| 3 | <!-- Standard Head Part -->
|
---|
| 4 | <head>
|
---|
| 5 | <title>NUnit - Property</title>
|
---|
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
---|
| 7 | <meta http-equiv="Content-Language" content="en-US">
|
---|
| 8 | <link rel="stylesheet" type="text/css" href="nunit.css">
|
---|
| 9 | <link rel="shortcut icon" href="favicon.ico">
|
---|
| 10 | </head>
|
---|
| 11 | <!-- End Standard Head Part -->
|
---|
| 12 |
|
---|
| 13 | <body>
|
---|
| 14 |
|
---|
| 15 | <!-- Standard Header for NUnit.org -->
|
---|
| 16 | <div id="header">
|
---|
| 17 | <a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
|
---|
| 18 | <div id="nav">
|
---|
| 19 | <a href="http://www.nunit.org">NUnit</a>
|
---|
| 20 | <a class="active" href="index.html">Documentation</a>
|
---|
| 21 | </div>
|
---|
| 22 | </div>
|
---|
| 23 | <!-- End of Header -->
|
---|
| 24 |
|
---|
| 25 | <div id="content">
|
---|
| 26 |
|
---|
| 27 | <script language="JavaScript" src="codeFuncs.js" ></script> <!-- Do it this way for IE -->
|
---|
| 28 |
|
---|
| 29 | <h3>PropertyAttribute (NUnit 2.4)</h3>
|
---|
| 30 |
|
---|
| 31 | <p>The Property attribute provides a generalized approach to setting named
|
---|
| 32 | properties on any test case or fixture, using a name/value pair.</p>
|
---|
| 33 |
|
---|
| 34 | <p>In the example below, the fixture class MathTests is given a Location
|
---|
| 35 | value of 723 while the test case AdditionTest is given a Severity
|
---|
| 36 | of "Critical"</p>
|
---|
| 37 |
|
---|
| 38 | <h4>Example:</h4>
|
---|
| 39 |
|
---|
| 40 | <div class="code">
|
---|
| 41 |
|
---|
| 42 | <div class="langFilter">
|
---|
| 43 | <a href="javascript:Show('DD1')" onmouseover="Show('DD1')"><img src="img/langFilter.gif" width="14" height="14" alt="Language Filter"></a>
|
---|
| 44 | <div id="DD1" class="dropdown" style="display: none" onclick="Hide('DD1')">
|
---|
| 45 | <a href="javascript:ShowCS()">C#</a><br>
|
---|
| 46 | <a href="javascript:ShowVB()">VB</a><br>
|
---|
| 47 | <a href="javascript:ShowMC()">C++</a><br>
|
---|
| 48 | <a href="javascript:ShowJS()">J#</a><br>
|
---|
| 49 | </div>
|
---|
| 50 | </div>
|
---|
| 51 |
|
---|
| 52 | <pre class="cs">namespace NUnit.Tests
|
---|
| 53 | {
|
---|
| 54 | using System;
|
---|
| 55 | using NUnit.Framework;
|
---|
| 56 |
|
---|
| 57 | [TestFixture, Property("Location",723)]
|
---|
| 58 | public class MathTests
|
---|
| 59 | {
|
---|
| 60 | [Test, Property("Severity", "Critical")]
|
---|
| 61 | public void AdditionTest()
|
---|
| 62 | { /* ... */ }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | </pre>
|
---|
| 66 |
|
---|
| 67 | <pre class="vb">Imports System
|
---|
| 68 | Imports Nunit.Framework
|
---|
| 69 |
|
---|
| 70 | Namespace Nunit.Tests
|
---|
| 71 |
|
---|
| 72 | <TestFixture(), Property("Location",723)>
|
---|
| 73 | Public Class MathTests
|
---|
| 74 |
|
---|
| 75 | <Test(), Property("Severity","Critical")>
|
---|
| 76 | Public Sub AdditionTest()
|
---|
| 77 | ' ...
|
---|
| 78 | End Sub
|
---|
| 79 |
|
---|
| 80 | End Class
|
---|
| 81 |
|
---|
| 82 | End Namespace
|
---|
| 83 | </pre>
|
---|
| 84 |
|
---|
| 85 | <pre class="mc">#using <Nunit.Framework.dll>
|
---|
| 86 | using namespace System;
|
---|
| 87 | using namespace NUnit::Framework;
|
---|
| 88 |
|
---|
| 89 | namespace NUnitTests
|
---|
| 90 | {
|
---|
| 91 | [TestFixture, Property("Location",723)]
|
---|
| 92 | public __gc class MathTests
|
---|
| 93 | {
|
---|
| 94 | [Test, Property("Severity","Critical")] void AdditionTest();
|
---|
| 95 | };
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | #include "cppsample.h"
|
---|
| 99 |
|
---|
| 100 | namespace NUnitTests {
|
---|
| 101 | // ...
|
---|
| 102 | }
|
---|
| 103 | </pre>
|
---|
| 104 |
|
---|
| 105 | <pre class="js">package NUnit.Tests;
|
---|
| 106 |
|
---|
| 107 | import System.*;
|
---|
| 108 | import NUnit.Framework.TestFixture;
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | /** @attribute NUnit.Framework.TestFixture() */
|
---|
| 112 | /** @attribute NUnit.Framework.Property("Location",723) */
|
---|
| 113 | public class MathTests
|
---|
| 114 | {
|
---|
| 115 | /** @attribute NUnit.Framework.Test() */
|
---|
| 116 | /** @attribute NUnit.Framework.Property("Severity","Critical") */
|
---|
| 117 | public void AdditionTest()
|
---|
| 118 | { /* ... */ }
|
---|
| 119 | }
|
---|
| 120 | </pre>
|
---|
| 121 |
|
---|
| 122 | </div>
|
---|
| 123 |
|
---|
| 124 | <h4>Usage Note</h4>
|
---|
| 125 |
|
---|
| 126 | <p>The PropertyAttribute is not used for any purpose by NUnit itself, but
|
---|
| 127 | it does display them in the XML output file and in the Test Properties
|
---|
| 128 | dialog of the gui.</p>
|
---|
| 129 |
|
---|
| 130 | <p>It is
|
---|
| 131 | possible to write extensions that access the value of specific properties.
|
---|
| 132 | It is also possible to access the value of properties from within a test
|
---|
| 133 | using reflection.</p>
|
---|
| 134 |
|
---|
| 135 | <h3>Custom Property Attributes</h3>
|
---|
| 136 |
|
---|
| 137 | <p>Users can define custom
|
---|
| 138 | attributes that derive from <b>PropertyAttribute</b> and have them
|
---|
| 139 | recognized by NUnit. PropertyAttribute provides a protected constructor
|
---|
| 140 | that takes the value of the property and sets the property name to the
|
---|
| 141 | name of the derived class. NUnit itself uses this facility: some of
|
---|
| 142 | it's specialized attributes are actually specializations of <b>PropertyAttribute</b>.
|
---|
| 143 |
|
---|
| 144 | <p>Here's an example that creates a Severity property. It works
|
---|
| 145 | just like any other property, but has a simpler syntax and is type-safe.
|
---|
| 146 | A test reporting system might make use of the property to provide special reports.
|
---|
| 147 |
|
---|
| 148 | <div class=code><pre>
|
---|
| 149 | public enum SeverityLevel
|
---|
| 150 | {
|
---|
| 151 | Critical,
|
---|
| 152 | Major,
|
---|
| 153 | Normal,
|
---|
| 154 | Minor
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
|
---|
| 158 | public class SeverityAttribute : PropertyAttribute
|
---|
| 159 | {
|
---|
| 160 | public SeverityAttribute( SeverityLevel level )
|
---|
| 161 | : base( level );
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | ...
|
---|
| 165 |
|
---|
| 166 | [Test, Severity( SeverityLevel.Critical)]
|
---|
| 167 | public void MyTest()
|
---|
| 168 | { /*...*/ }
|
---|
| 169 | </pre></div>
|
---|
| 170 |
|
---|
| 171 | <p>Beginning with NUnit 2.5, a property attribute is able to contain
|
---|
| 172 | multiple name/value pairs. This capability is not exposed publicly
|
---|
| 173 | but may be used by derived property classes. NUnit uses this
|
---|
| 174 | feature itself for certain attributes. See, for example,
|
---|
| 175 | <a href="requiresThread.html">RequiresThreadAttribute</a>.
|
---|
| 176 |
|
---|
| 177 | </div>
|
---|
| 178 |
|
---|
| 179 | <!-- Submenu -->
|
---|
| 180 | <div id="subnav">
|
---|
| 181 | <ul>
|
---|
| 182 | <li><a href="index.html">NUnit 2.5.10</a></li>
|
---|
| 183 | <ul>
|
---|
| 184 | <li><a href="getStarted.html">Getting Started</a></li>
|
---|
| 185 | <li><a href="assertions.html">Assertions</a></li>
|
---|
| 186 | <li><a href="constraintModel.html">Constraints</a></li>
|
---|
| 187 | <li><a href="attributes.html">Attributes</a></li>
|
---|
| 188 | <ul>
|
---|
| 189 | <li><a href="category.html">Category</a></li>
|
---|
| 190 | <li><a href="combinatorial.html">Combinatorial</a></li>
|
---|
| 191 | <li><a href="culture.html">Culture</a></li>
|
---|
| 192 | <li><a href="datapoint.html">Datapoint(s)</a></li>
|
---|
| 193 | <li><a href="description.html">Description</a></li>
|
---|
| 194 | <li><a href="exception.html">Exception</a></li>
|
---|
| 195 | <li><a href="explicit.html">Explicit</a></li>
|
---|
| 196 | <li><a href="ignore.html">Ignore</a></li>
|
---|
| 197 | <li><a href="maxtime.html">Maxtime</a></li>
|
---|
| 198 | <li><a href="pairwise.html">Pairwise</a></li>
|
---|
| 199 | <li><a href="platform.html">Platform</a></li>
|
---|
| 200 | <li id="current"><a href="property.html">Property</a></li>
|
---|
| 201 | <li><a href="random.html">Random</a></li>
|
---|
| 202 | <li><a href="range.html">Range</a></li>
|
---|
| 203 | <li><a href="repeat.html">Repeat</a></li>
|
---|
| 204 | <li><a href="requiredAddin.html">RequiredAddin</a></li>
|
---|
| 205 | <li><a href="requiresMTA.html">Requires MTA</a></li>
|
---|
| 206 | <li><a href="requiresSTA.html">Requires STA</a></li>
|
---|
| 207 | <li><a href="requiresThread.html">Requires Thread</a></li>
|
---|
| 208 | <li><a href="sequential.html">Sequential</a></li>
|
---|
| 209 | <li><a href="setCulture.html">SetCulture</a></li>
|
---|
| 210 | <li><a href="setUICulture.html">SetUICulture</a></li>
|
---|
| 211 | <li><a href="setup.html">Setup</a></li>
|
---|
| 212 | <li><a href="setupFixture.html">SetupFixture</a></li>
|
---|
| 213 | <li><a href="suite.html">Suite</a></li>
|
---|
| 214 | <li><a href="teardown.html">Teardown</a></li>
|
---|
| 215 | <li><a href="test.html">Test</a></li>
|
---|
| 216 | <li><a href="testCase.html">TestCase</a></li>
|
---|
| 217 | <li><a href="testCaseSource.html">TestCaseSource</a></li>
|
---|
| 218 | <li><a href="testFixture.html">TestFixture</a></li>
|
---|
| 219 | <li><a href="fixtureSetup.html">TestFixtureSetUp</a></li>
|
---|
| 220 | <li><a href="fixtureTeardown.html">TestFixtureTearDown</a></li>
|
---|
| 221 | <li><a href="theory.html">Theory</a></li>
|
---|
| 222 | <li><a href="timeout.html">Timeout</a></li>
|
---|
| 223 | <li><a href="values.html">Values</a></li>
|
---|
| 224 | <li><a href="valueSource.html">ValueSource</a></li>
|
---|
| 225 | </ul>
|
---|
| 226 | <li><a href="runningTests.html">Running Tests</a></li>
|
---|
| 227 | <li><a href="extensibility.html">Extensibility</a></li>
|
---|
| 228 | <li><a href="releaseNotes.html">Release Notes</a></li>
|
---|
| 229 | <li><a href="samples.html">Samples</a></li>
|
---|
| 230 | <li><a href="license.html">License</a></li>
|
---|
| 231 | </ul>
|
---|
| 232 | </ul>
|
---|
| 233 | </div>
|
---|
| 234 | <!-- End of Submenu -->
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | <!-- Standard Footer for NUnit.org -->
|
---|
| 238 | <div id="footer">
|
---|
| 239 | Copyright © 2010 Charlie Poole. All Rights Reserved.
|
---|
| 240 | </div>
|
---|
| 241 | <!-- End of Footer -->
|
---|
| 242 |
|
---|
| 243 | </body>
|
---|
| 244 | </html>
|
---|