5

Sto tentando di aggiungere il plug-in del generatore di test CodedUI in SpecFlow 1.9. Sto cercando di utilizzare la nuova registrazione del plug-in, dal momento che il vecchio tipo di registrazione causa tutti i tipi di problemi nella nostra soluzione, dal momento che preferiamo non inserire il generatore personalizzato nella directory di installazione di Specflow (che è diversa su ogni macchina degli sviluppatori) .Custom SpecFlow TestGeneratorProvider registrazione causa NullReferenceException alla generazione di test

Ho iniziato aggiungendo il nuovo Specflow.CustomPlugin NuGet package a una libreria di classi .NET 4.0.

Sulla base dei campioni per il generatore CodedUI, ho ricostruito questo insieme:

using TechTalk.SpecFlow.Infrastructure; 
[assembly: GeneratorPlugin(typeof(CodedUIGeneratorProvider.Generator.SpecFlowPlugin.CodedUIGeneratorPlugin))] 

namespace CodedUIGeneratorProvider.Generator.SpecFlowPlugin 
{ 
    using System.CodeDom; 

    using BoDi; 

    using TechTalk.SpecFlow.Generator; 
    using TechTalk.SpecFlow.Generator.Configuration; 
    using TechTalk.SpecFlow.Generator.Plugins; 
    using TechTalk.SpecFlow.Generator.UnitTestProvider; 
    using TechTalk.SpecFlow.UnitTestProvider; 
    using TechTalk.SpecFlow.Utils; 

    /// <summary> 
    /// The CodedUI generator plugin. 
    /// </summary> 
    public class CodedUIGeneratorPlugin : IGeneratorPlugin 
    { 
     /// <summary> 
     /// The register dependencies. 
     /// </summary> 
     /// <param name="container"> 
     /// The container. 
     /// </param> 
     public void RegisterDependencies(ObjectContainer container) 
     { 
     } 

     /// <summary> 
     /// The register customizations. 
     /// </summary> 
     /// <param name="container"> 
     /// The container. 
     /// </param> 
     /// <param name="generatorConfiguration"> 
     /// The generator configuration. 
     /// </param> 
     public void RegisterCustomizations(ObjectContainer container, SpecFlowProjectConfiguration generatorConfiguration) 
     { 
      container.RegisterTypeAs<CodedUIGeneratorProvider, IUnitTestGeneratorProvider>("default"); 
      container.RegisterTypeAs<MsTest2010RuntimeProvider, IUnitTestRuntimeProvider>("default"); 
     } 

     /// <summary> 
     /// The register configuration defaults. 
     /// </summary> 
     /// <param name="specFlowConfiguration"> 
     /// The spec flow configuration. 
     /// </param> 
     public void RegisterConfigurationDefaults(SpecFlowProjectConfiguration specFlowConfiguration) 
     { 
     } 
    } 

    /// <summary> 
    /// The CodedUI generator. 
    /// </summary> 
    public class CodedUIGeneratorProvider : MsTest2010GeneratorProvider 
    { 
     /// <summary> 
     /// Initializes a new instance of the <see cref="CodedUiGeneratorProvider"/> class. 
     /// </summary> 
     /// <param name="codeDomHelper"> 
     /// The code dom helper. 
     /// </param> 
     public CodedUIGeneratorProvider(CodeDomHelper codeDomHelper) 
      : base(codeDomHelper) 
     { 
     } 

     /// <summary> 
     /// The set test class. 
     /// </summary> 
     /// <param name="generationContext"> 
     /// The generation context. 
     /// </param> 
     /// <param name="featureTitle"> 
     /// The feature title. 
     /// </param> 
     /// <param name="featureDescription"> 
     /// The feature description. 
     /// </param> 
     public override void SetTestClass(TestClassGenerationContext generationContext, string featureTitle, string featureDescription) 
     { 
      base.SetTestClass(generationContext, featureTitle, featureDescription); 

      foreach (CodeAttributeDeclaration declaration in generationContext.TestClass.CustomAttributes) 
      { 
       if (declaration.Name == "Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute") 
       { 
        generationContext.TestClass.CustomAttributes.Remove(declaration); 
        break; 
       } 
      } 

      generationContext.TestClass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference("Microsoft.VisualStudio.TestTools.UITesting.CodedUITestAttribute"))); 
     } 
    } 
} 

E quando si cerca di configurarlo come segue:

<specFlow> 
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --> 
    <plugins> 
     <add name="CodedUIGeneratorProvider" path="." type="GeneratorAndRuntime"/> 
    </plugins> 
    <generator dependencies="CodedUIGeneratorProvider"/> 
    <runtime dependencies="CodedUIGeneratorProvider"/> 
    </specFlow> 

ottengo il seguente messaggio di errore:

error Generation error: SpecFlow configuration error -> The value of the property 'dependencies' cannot be parsed. The error is: Object reference not set to an instance of an object.

L'errore è causato all'interno del codice di configurazione SpecFlow a quanto pare, la traccia dello stack è la seguente:

System.Configuration.dll!System.Configuration.ConfigurationProperty.ConvertFromString(string value) + 0x2a bytes 
System.Configuration.dll!System.Configuration.ConfigurationElement.DeserializePropertyValue(System.Configuration.ConfigurationProperty prop, System.Xml.XmlReader reader) + 0x36 bytes 
System.Configuration.dll!System.Configuration.ConfigurationElement.DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey) + 0x221 bytes 
System.Configuration.dll!System.Configuration.ConfigurationElement.DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey) + 0x78e bytes 
System.Configuration.dll!System.Configuration.ConfigurationSection.DeserializeSection(System.Xml.XmlReader reader) + 0x3c bytes 
TechTalk.SpecFlow.dll!TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler.CreateFromXml(string xmlContent) + 0xb0 bytes 
TechTalk.SpecFlow.Generator.dll!TechTalk.SpecFlow.Generator.Configuration.GeneratorConfigurationProvider.LoadConfiguration(TechTalk.SpecFlow.Generator.Interfaces.SpecFlowConfigurationHolder configurationHolder, TechTalk.SpecFlow.Generator.Configuration.SpecFlowProjectConfiguration configuration) + 0x41 bytes 
TechTalk.SpecFlow.Vs2010Integration.dll!TechTalk.SpecFlow.Vs2010Integration.Generator.VsGeneratorInfoProvider.GenGeneratorConfig() + 0x52 bytes 
TechTalk.SpecFlow.Vs2010Integration.dll!TechTalk.SpecFlow.Vs2010Integration.Generator.VsGeneratorInfoProvider.GetGeneratorInfo() + 0x3a bytes 
TechTalk.SpecFlow.IdeIntegration.dll!TechTalk.SpecFlow.IdeIntegration.Generator.RemoteGeneratorServices.GetGeneratorInfo() Line 38 + 0x9 bytesC# 
TechTalk.SpecFlow.IdeIntegration.dll!TechTalk.SpecFlow.IdeIntegration.Generator.RemoteGeneratorServices.GetTestGeneratorFactoryForCreate() Line 43 + 0xa bytesC# 
TechTalk.SpecFlow.IdeIntegration.dll!TechTalk.SpecFlow.IdeIntegration.Generator.GeneratorServices.CreateTestGenerator() Line 24 + 0xa bytesC# 
TechTalk.SpecFlow.IdeIntegration.dll!TechTalk.SpecFlow.IdeIntegration.Generator.IdeSingleFileGenerator.GenerateCode(string inputFilePath, string inputFileContent, TechTalk.SpecFlow.IdeIntegration.Generator.GeneratorServices generatorServices, TechTalk.SpecFlow.Generator.Interfaces.ProjectSettings projectSettings) Line 38 + 0x29 bytesC# 
TechTalk.SpecFlow.IdeIntegration.dll!TechTalk.SpecFlow.IdeIntegration.Generator.IdeSingleFileGenerator.Generate(string inputFilePath, string inputFileContent, TechTalk.SpecFlow.IdeIntegration.Generator.GeneratorServices generatorServices, TechTalk.SpecFlow.Utils.CodeDomHelper codeDomHelper, TechTalk.SpecFlow.Generator.Interfaces.ProjectSettings projectSettings) Line 23 + 0x12 bytesC# 
TechTalk.SpecFlow.IdeIntegration.dll!TechTalk.SpecFlow.IdeIntegration.Generator.IdeSingleFileGenerator.GenerateFile(string inputFilePath, string outputFilePath, System.Func<TechTalk.SpecFlow.IdeIntegration.Generator.GeneratorServices> generatorServicesProvider, System.Func<string,string> inputFileContentProvider, System.Action<string,string> outputFileContentWriter) Line 92 + 0x13 bytesC# 
TechTalk.SpecFlow.Vs2010Integration.dll!TechTalk.SpecFlow.VsIntegration.SingleFileGenerator.SpecFlowSingleFileGeneratorBase.GenerateInternal(string inputFilePath, string inputFileContent, EnvDTE.Project project, string defaultNamespace, System.Action<TechTalk.SpecFlow.VsIntegration.SingleFileGenerator.SingleFileGeneratorError> onError, out string generatedContent) + 0x18e bytes 
TechTalk.SpecFlow.Vs2010Integration.dll!TechTalk.SpecFlow.VsIntegration.SingleFileGenerator.SingleFileGeneratorBase.Generate(string inputFilePath, string inputFileContents, string defaultNamespace, System.IntPtr[] rgbOutputFileContents, out uint pcbOutput, Microsoft.VisualStudio.Shell.Interop.IVsGeneratorProgress generateProgress) + 0xc3 bytes 
[Native to Managed Transition] 

Quali riflettore mi dice che può solo significare che TechTalk.SpecFlow.IdeIntegration.Generator.RemoteGeneratorServices.generatorInfoProvider è null.

Sono in perdita su come risolvere questo problema. C'è pochissima documentazione disponibile per risolvere questo problema.

la domanda:

Se riesco a far funzionare tutto questo sarei molto felice. altenativamente mi piacerebbe vedere un modo per configurare il 'vecchio modo' senza dover mettere i file nella directory di installazione di SpecFlow.

risposta

1

Per gli sviluppatori che vengono qui con SpecFlow 2.1.0, si prega di aggiungere il seguente alla classe CodedUIGeneratorProvider sopra:

public void Initialize(GeneratorPluginEvents generatorPluginEvents, GeneratorPluginParameters generatorPluginParameters) 
    { 
     generatorPluginEvents.CustomizeDependencies += GeneratorPluginEvents_CustomizeDependencies; 
    } 

    private void GeneratorPluginEvents_CustomizeDependencies(object sender, CustomizeDependenciesEventArgs eventArgs) 
    { 
     eventArgs.ObjectContainer.RegisterTypeAs<CodedUIGeneratorProvider, IUnitTestGeneratorProvider>(); 
    } 

L'interfaccia dell'interfaccia CustomPlugin è cambiato, rendendo necessario collegarsi agli eventi di personalizzazione per ottenere la personalizzazione registrata.

Vedi https://github.com/techtalk/SpecFlow/wiki/Plugins

5

ho trovato il problema :)

Nel metodo Registrati personalizzazioni del plugin, non si registra utilizzando un nome, basta usare il metodo RegisterTypeAs:

public void RegisterCustomizations(ObjectContainer container, SpecFlowProjectConfiguration generatorConfiguration) 
{ 
    container.RegisterTypeAs<CodedUIGeneratorProvider, IUnitTestGeneratorProvider>(); 
} 

Poi la configurazione sembra molto semplice , in questo modo:

<specFlow> 
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --> 
    <plugins> 
    <add name="CodedUIGeneratorProvider" path="." type="Generator"/> 
    </plugins> 
</specFlow> 

in questo modo è possibile eliminare il gruppo di plug-in (che deve essere nominato *.SpecflowPlugin.dll) nella directory del progetto, o utilizzare un parente p ath dalla directory del progetto e impostarlo nella proprietà path=".\Lib" dell'elemento plugins\add.

Per ulteriori informazioni si veda: http://blog.jessehouwing.nl/2013/04/creating-custom-unit-test-generator.html