Редактиране

Споделяне чрез


Attribute glossary

Attributes by namespace

PexAssumeNotNull

This attribute asserts that the governed value cannot be null. It can be attached to:

  • a parameter of a parameterized test method

    // assume foo is not null
    [PexMethod]
    public void SomeTest([PexAssumeNotNull]IFoo foo, ...) {}
    
  • a field

    public class Foo {
       // this field should not be null
       [PexAssumeNotNull]
       public object Bar;
    }
    
  • a type

    // never consider null for Foo types
    [PexAssumeNotNull]
    public class Foo {}
    

It can also be attached to a test assembly, test fixture or test method; in this case the first arguments must indicate to which field or type the assumptions apply. When the attribute applies to a type, it applies to all fields with this formal type.

PexClass

This attribute marks a class that contains explorations. It is the equivalent of the MSTest TestClassAttribute (or the NUnit TestFixtureAttribute). This attribute is optional.

The classes marked with PexClass must be default constructible:

  • publicly exported type
  • default constructor
  • not abstract

If the class does not meet those requirements, an error is reported and the exploration fails.

It is also strongly advised to make those classes partial so that IntelliTest can generate new tests that are part of the class, but in a separate file. This approach solves many problems due to visibility and is a typical technique in C#.

Additional suite and categories:

[TestClass] // MSTest test fixture attribute
[PexClass(Suite = "checkin")] // fixture attribute
public partial class MyTests { ... }

Specifying the type under test:

[PexClass(typeof(Foo))] // this is a test for Foo
public partial class FooTest { ... }

The class may contain methods annotated with PexMethod. IntelliTest also understands set up and tear down methods.

PexGenericArguments

This attribute provides a type tuple for instantiating a generic parameterized unit test.

PexMethod

This attribute marks a method as a parameterized unit test. The method must reside within a class marked with the PexClass attribute.

IntelliTest will generate traditional, parameterless tests, which call the parameterized unit test with different parameters.

The parameterized unit test:

  • must be an instance method
  • must be visible to the test class into which the generated tests are placed according to the Settings Waterfall
  • may take any number of parameters
  • may be generic

Example

[PexClass]
public partial class MyTests {
     [PexMethod]
     public void MyTest(int i)
     { ... }
}

PexExplorationAttributeBase

More information

PexAssemblySettings

This attribute can be set at the assembly level to override default setting values for all explorations.

using Microsoft.Pex.Framework;
// overriding the test framework selection
[assembly: PexAssemblySettings(TestFramework = "MSTestv2")]

PexAssemblyUnderTest

This attribute specifies an assembly that is being tested by the current test project.

[assembly: PexAssemblyUnderTest("MyAssembly")]

PexInstrumentAssemblyAttribute

This attribute is used to specify an assembly to be instrumented.

Example

using Microsoft.Pex.Framework;

// the assembly containing ATypeFromTheAssemblyToInstrument should be instrumented
[assembly: PexInstrumentAssembly(typeof(ATypeFromTheAssemblyToInstrument))]

// the assembly name can be used as well
[assembly: PexInstrumentAssembly("MyAssemblyName")]

PexUseType

This attribute tells IntelliTest that it can use a particular type to instantiate (abstract) base types or interfaces.

Example

[PexMethod]
[PexUseType(typeof(A))]
[PexUseType(typeof(B))]
public void MyTest(object testParameter)
{
     ... // IntelliTest will consider types A and B to instantiate 'testParameter'
}

PexAllowedException

If this attribute is attached to a PexMethod (or to a PexClass, it changes the default IntelliTest logic that indicates when tests fails. The test will not be considered as failed, even if it throws the specified exception.

Example

The following test specifies that the constructor of Stack may throw an ArgumentOutOfRangeException:

class Stack {
  int[] _elements;
  int _count;
  public Stack(int capacity) {
    if (capacity<0) throw new ArgumentOutOfRangeException();
    _elements = new int[capacity];
    _count = 0;
  }
  ...
}

The filter is attached to a fixture as follows (it can also be defined at the assembly or test level):

[PexMethod]
[PexAllowedException(typeof(ArgumentOutOfRangeException))]
class CtorTest(int capacity) {
  Stack s = new Stack(capacity); // may throw ArgumentOutOfRangeException
}

PexAllowedExceptionFromAssembly

More information

PexAllowedExceptionFromType

More information

PexAllowedExceptionFromTypeUnderTest

More information

Got feedback?

Post your ideas and feature requests on Developer Community.