phuccaoca123 / mb-unit

Automatically exported from code.google.com/p/mb-unit
0 stars 0 forks source link

Integrated property tester #21

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Writing tests for Plain old .Net Classes is pretty boring.  To get coverage
you have to get/set each property and check for exceptions.  Several people
have addressed this problem before with reflection-based tweaks but they're
a little clumsy.

We should provide something out of the box that's better.  For example, we
can add a new attribute that automatically provides test cases for all
public properties of some type.  For properties that require extra work, we
can define a naming convention so allow various aspects of the generated
tests to be configured.  We might also be able to provide declarative
specifications of property contracts like exception behavior.

eg.

[TestFixture]
[GenerateTestsForProperties(typeof(SomeType))]
[PropertyContract(typeof(SomeType), "Foo",
CommonContracts.ThrowsArgumentNullExceptionIfValueIsNull)]
public class SomeTypeTest
{
    [Test]
    public void PropertyFoo()
    {
        // overrides the auto-generated test for property Foo of SomeType.
    }

    [Factory]
    public SomeValueType CreateSomeValueType()
    {
        // return a dummy instance of a type used in property setter
    }
}

Original issue reported on code.google.com by jeff.br...@gmail.com on 27 Aug 2007 at 7:58

GoogleCodeExporter commented 8 years ago
Example: http://www.thejoyofcode.com/Automatically_Unit_Test_your_Model.aspx

Original comment by jeff.br...@gmail.com on 8 Sep 2007 at 10:19

GoogleCodeExporter commented 8 years ago

Original comment by jeff.br...@gmail.com on 30 Oct 2007 at 2:24

GoogleCodeExporter commented 8 years ago

Original comment by jeff.br...@gmail.com on 17 Mar 2008 at 10:57

GoogleCodeExporter commented 8 years ago
Yann, this is in line with the contract verifier stuff we've been discussing.

Original comment by jeff.br...@gmail.com on 11 Nov 2008 at 8:49

GoogleCodeExporter commented 8 years ago
Could be done as a contract verifier of sorts...

Maybe use lambdas or expression trees to help select properties of interest for 
testing.

Original comment by jeff.br...@gmail.com on 31 Mar 2009 at 3:57

GoogleCodeExporter commented 8 years ago
Actually, I am working on it right now. 
I expect to commit an experimental version during the week.
In its most simple form, the syntax should be:

[VerifyContract]
public readonly IContract FooAccessorTests = new AccessorContract<Sample, Foo>
{
  Getter = target => target.Foo,
  Setter = (target, value) => target.Foo = value,
  ValidValues = { new Foo(123), new Foo(456), new Foo(789) },
};

Various customizations are possible as well:

// To check automatically for ArgumentNullException.
AcceptNullValue = false|true,

// To provide a custom instance if no default constructor is available or if 
the 
user wants it for any reason.
GetDefaultInstance = () => new Sample("Hello"), 

// And to check for various expected exceptions when settings invalid values.
InvalidValues =
{
  { typeof(ArgumentOutOfRangeException), new Foo(-123), new Foo(-456) },
  { typeof(ArgumentException), new Foo(666) }
}

Original comment by Yann.Tre...@gmail.com on 31 Mar 2009 at 6:21

GoogleCodeExporter commented 8 years ago
I wonder if we can combine several properties together.

new AccessorContract<...>
{
    Properties =
    {
        Getter = ...
        Setter = ...
        ValidValues = ....
        InvalidValues = ...
    },
    {
        Getter = ...
        Setter = ...
        ValidValues = ....
        InvalidValues = ...
    },
    {
        Name = "PropertyNameToAccessViaReflection"
    }
}

Doesn't look like it will work with the type inference you have on Foo though.

Original comment by jeff.br...@gmail.com on 31 Mar 2009 at 9:03

GoogleCodeExporter commented 8 years ago
Mmh... That possibility to combine several properties at once is a good idea.

I was relunctant to let the user identify a property by its name. It tends to 
be a 
fragile declaration which hardly survives any rename refactorisation. But to 
let 
both the possibilities (by the name and by explicit expressions defining getter 
and 
setter) is a good point too.

Well. I think you have too many good ideas. Are you not tired at the end of 
day? :D

Original comment by Yann.Tre...@gmail.com on 1 Apr 2009 at 1:00

GoogleCodeExporter commented 8 years ago
AccessorContract<TTarget, TValue> is available.

Original comment by Yann.Tre...@gmail.com on 6 Apr 2009 at 6:26