Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Custom DataCollector not found #248

Open
davsucks opened this issue Apr 27, 2021 · 1 comment
Open

Custom DataCollector not found #248

davsucks opened this issue Apr 27, 2021 · 1 comment

Comments

@davsucks
Copy link

Hi there,
I'm trying to add a custom log collector to our tests and I'm not able to get the collector recognized. It very simply just collects failed test cases and prints them out at the end.

I've followed the examples found over at analyize.md and have the following:

// ./LogCollector/LogCollector.cs
using System.Collections.Generic;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;

namespace LogCollector
{
    [DataCollectorFriendlyName("LogCollector")]
    [DataCollectorTypeUri("datacollector://LogCollector")]
    public class LogCollector : DataCollector
    {
        private readonly List<TestCase> _failedTests = new List<TestCase>();
        private DataCollectionLogger _logger;

        public override void Initialize(XmlElement configurationElement, DataCollectionEvents events,
            DataCollectionSink dataSink,
            DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext)
        {
            events.TestCaseEnd += TestCaseEnd_Handler;
            events.SessionEnd += SessionEnd_Handler;
            _logger = logger;
        }

        private void SessionEnd_Handler(object? sender, SessionEndEventArgs e)
        {
            _failedTests.ForEach(testCase =>
                _logger.LogError(new DataCollectionContext(testCase), testCase.FullyQualifiedName)
            );
        }

        private void TestCaseEnd_Handler(object? sender, TestCaseEndEventArgs testCaseEndEventArgs)
        {
            if (testCaseEndEventArgs.TestOutcome == TestOutcome.Failed)
            {
                _failedTests.Add(testCaseEndEventArgs.TestElement);
            }
        }
    }
}

and my .runsettings file looks like this:

<!-- ./.runsettings -->

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
        <RunConfiguration>
            <TestAdaptersPaths>./LogCollector/LogCollector.cs</TestAdaptersPaths>
        </RunConfiguration>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="LogCollector" uri="datacollector://LogCollector"/>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

When I run the tests with dotnet test --collector LogCollector I'm faced with the error:

Data collection : Unable to find a datacollector with friendly name 'LogCollector'.
Data collection : Could not find data collector 'LogCollector'

Does anyone have any insight into how I can get the data collector to be picked up by the test runner?

Thanks!

@nohwnd
Copy link
Member

nohwnd commented May 27, 2021

Hello,
when your data collector is not picked up the most common mistake is the name of the assembly. It need to end with Collector.dll as outlined here:
https://github.com/Microsoft/vstest-docs/blob/master/docs/extensions/datacollector.md#extend-datacollector

You are also providing test adapter path to a .cs file. But that is not how it works. Your path should be to a directory, that contains your *Collector.dll. Or if placed right next to your test assembly it will be picked up automatically (this is how testadapter like mstest.testadapter.dll get picked up).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants