Ok, I guess it is a little complicated.  My basic suggestion is that at this
point you might as well use the framework to explore the tests.

Option 1: Leverage the whole framework to explore tests in particular
types.  This produces a sequence of TestDiscoveredMessage objects that
describe the tests in detail.

public void DescribeTests(IEnumerable<Type> types, IMessageSink consumer)
{
    var testFrameworkManager =
RuntimeAccessor.ServiceLocator.Resolve<ITestFrameworkManager>();
    var testFrameworkSelector = new TestFrameworkSelector()
    {
        Filter = testFrameworkHandle => testFrameworkHandle.Id ==
"MbUnit.TestFramework",
        FallbackMode = TestFrameworkFallbackMode.Approximate
    };
    var testDriver =
testFrameworkManager.GetTestDriver(testFrameworkSelector, logger);

    var reflectionPolicy = Reflector.NativeReflectionPolicy;
    var codeElements = new List<ICodeElementInfo>();
    foreach (Type type in types)
        codeElements.Add(Reflector.Wrap(type));

    var testExplorationOptions = new TestExplorationOptions();
    testDriver.Describe(reflectionPolicy, codeElements,
testExplorationOptions, consumer, NullProgressMonitor.CreateInstance());
}

public class MyConsumer : IMessageSink
{
    public void Publish(Message message)
    {
        if (message is TestDiscoveredMessage)
          // do something with the test that was discovered...
    }
}

Option 2: Use the pattern test framework directly.  This produces a
TestModel that contains a tree of PatternTests.

public TestModel BuildTestModel(Assembly assembly, IEnumerable<Type> types)
{
    var reflectionPolicy = Reflector.NativeReflectionPolicy.
    var testModelBuilder = new DefaultTestModelBuilder(reflectionPolicy,
(PatternTestModel)TestModel);
    var evaluator = new DefaultPatternEvaluator(testModelBuilder,
DeclarativePatternResolver.Instance);

    IPatternScope rootScope = evaluator.RootScope;
    IAssemblyInfo assemblyInfo = Reflector.Wrap(assembly);

    rootScope.Consume(assemblyInfo, true,
TestAssemblyPatternAttribute.DefaultInstance);

    foreach (Type type in types)
    {
        ITypeInfo typeInfo = Reflector.Wrap(type);

        foreach (IPatternScope scope in evaluator.GetScopes(assemblyInfo))
            scope.PopulateDeferredComponents(typeInfo);
    }

    testModelBuilder.ApplyDeferredActions();
    return testModelBuilder.TestModel;
}


(Incidentally, the refactoring I have shelved for now aims to make Option 2
super duper easy and to make PatternTest and TestCase effectively derive
from the same thing.)

Jeff.

On Wed, Mar 17, 2010 at 11:58 AM, mark Kharitonov <[email protected]
> wrote:

> I do not quite understand which Consume method do you mean. Neither *
> MetadataPatternAttribute<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Gallio:3.1.0.0:eb9cfa67ee6ab36e/Gallio.Framework.Pattern.MetadataPatternAttribute>
> * nor 
> *MetadataAttribute<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://MbUnit:3.1.0.0:eb9cfa67ee6ab36e/MbUnit.Framework.MetadataAttribute>
> *type override the Consume method and the one they inherit is empty. Can
> you elaborate your reply a bit?
> Thanks.
>
>
> On Wed, Mar 17, 2010 at 8:54 PM, Jeff Brown <[email protected]> wrote:
>
>> Have you considered calling the Consume method yourself and building up a
>> TestModel?
>>
>> Jeff.
>>
>> On Wed, Mar 17, 2010 at 11:47 AM, mark Kharitonov <
>> [email protected]> wrote:
>>
>>> Hi Jeff.
>>> Thanks for the prompt reply.
>>>
>>> What are the reasons to hide the MetadataPatternAttribute.GetMetadata()
>>> method? I mean, by just raising its scope to public my scenario would be
>>> much simpler to implement...
>>>
>>>
>>>
>>> On Wed, Mar 17, 2010 at 8:18 PM, Jeff Brown <[email protected]>wrote:
>>>
>>>> In general, you'll find that several attributes add metadata without
>>>> being descendants of MetadataAttribute.  For example, [Ignore] and
>>>> [Explicit] add IgnoreReason and ExplicitReason metadata, iirc.  Also [Test]
>>>> can have a Description which is encoded as Description metadata.
>>>>
>>>> Your use case just isn't something that the framework was intended to
>>>> support.  Long term, I'd like to unify the TestCase test construction
>>>> mechanism with the attribute-based test construction mechanism more.  That
>>>> will help your use-case but it's not going to happen immediately.
>>>>
>>>> Sorry,
>>>> Jeff.
>>>>
>>>>
>>>> On Wed, Mar 17, 2010 at 7:54 AM, Mark Kharitonov <
>>>> [email protected]> wrote:
>>>>
>>>>> Dear ladies and sirs.
>>>>>
>>>>> In my scenario I create dynamic tests, where each test wraps the
>>>>> respective statically compiled test. It is essential, that any
>>>>> metadata found on the static test will be copied to the respective
>>>>> dynamic test.
>>>>>
>>>>>  Quite conveniently, the TestCase type has Metadata dictionary. So, I
>>>>> execute a code like this:
>>>>>
>>>>> var test = new TestCase(...);
>>>>> var metadataAttrs =
>>>>>
>>>>> (MetadataAttribute[])methodInfo.GetCustomAttributes(typeof(MetadataAttribute),
>>>>> false);
>>>>> metadataAttrs.ForEach(m => test.Metadata.Add(m.MetadataKey,
>>>>> m.MetadataValue));
>>>>>
>>>>> BUT, CategoryAttribute is left out, because it does not derive from
>>>>> MetadataAttribute, but from MetadataPatternAttribute!
>>>>>
>>>>> My question is why? The Category is still metadata and it is treated
>>>>> internally as such. When I change the last line of the aforementioned
>>>>> code sample to this:
>>>>>
>>>>>      metadataAttrs.ForEach(i =>
>>>>>      {
>>>>>        var c = i as CategoryAttribute;
>>>>>        if (c == null)
>>>>>        {
>>>>>          var m = (MetadataAttribute)i;
>>>>>          test.Metadata.Add(m.MetadataKey, m.MetadataValue);
>>>>>        }
>>>>>        else
>>>>>        {
>>>>>          test.Metadata.Add("Category", c.Category);
>>>>>        }
>>>>>      });
>>>>>
>>>>> The dynamic tests are now treated as though they had the Category
>>>>> attribute, when what I did is just added the "Category" metadata key
>>>>> corresponding to the Category attribute.
>>>>>
>>>>> This Category-not-Metadata business makes the code more ugly.
>>>>>
>>>>> Another solution would be to raise the scope of the
>>>>> MetadataPatternAttribute.GetMetadata() method from protected to
>>>>> public.
>>>>>
>>>>> What do you think?
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "MbUnit.User" group.
>>>>> To post to this group, send email to [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected]<mbunituser%[email protected]>
>>>>> .
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/mbunituser?hl=en.
>>>>>
>>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "MbUnit.User" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<mbunituser%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/mbunituser?hl=en.
>>>>
>>>
>>>
>>>
>>> --
>>> Be well and prosper.
>>> ==============================
>>> "There are two kinds of people.Those whose guns are loaded and those who
>>> dig."
>>>   ("The good, the bad and the ugly")
>>> So let us drink for our guns always be loaded.
>>>
>>>  --
>>> You received this message because you are subscribed to the Google Groups
>>> "MbUnit.User" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<mbunituser%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/mbunituser?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "MbUnit.User" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<mbunituser%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/mbunituser?hl=en.
>>
>
>
>
> --
> Be well and prosper.
> ==============================
> "There are two kinds of people.Those whose guns are loaded and those who
> dig."
>   ("The good, the bad and the ugly")
> So let us drink for our guns always be loaded.
>
>  --
> You received this message because you are subscribed to the Google Groups
> "MbUnit.User" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<mbunituser%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/mbunituser?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"MbUnit.User" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/mbunituser?hl=en.

Reply via email to