Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type or namespace [TestNamespace]_XunitAssemblyFixture could not be found #1825

Closed
9 of 33 tasks
xavalon263 opened this issue Dec 18, 2019 · 55 comments · Fixed by #2431
Closed
9 of 33 tasks

Type or namespace [TestNamespace]_XunitAssemblyFixture could not be found #1825

xavalon263 opened this issue Dec 18, 2019 · 55 comments · Fixed by #2431

Comments

@xavalon263
Copy link

xavalon263 commented Dec 18, 2019

SpecFlow Version:

  • 3.1
  • 3.0
  • 2.4
  • 2.3
  • 2.2
  • 2.1
  • 2.0
  • 1.9

Used Test Runner

  • SpecFlow+Runner
  • MSTest
  • NUnit
  • Xunit

Version number:

Project Format of the SpecFlow project

  • Classic project format using packages.config
  • Classic project format using <PackageReference> tags
  • Sdk-style project format

.feature.cs files are generated using

  • SpecFlow.Tools.MsBuild.Generation NuGet package
  • SpecFlowSingleFileGenerator custom tool

Visual Studio Version

  • VS 2019
  • VS 2017
  • VS 2015

Enable SpecFlowSingleFileGenerator Custom Tool option in Visual Studio extension settings

  • Enabled
  • Disabled

Are the latest Visual Studio updates installed?

  • Yes
  • No, I use Visual Studio version <Major>.<Minor>.<Patch>

.NET Framework:

  • >= .NET 4.5
  • before .NET 4.5
  • .NET Core 2.0
  • .NET Core 2.1
  • .NET Core 2.2
  • .NET Core 3.0

Test Execution Method:

  • Visual Studio Test Explorer
  • TFS/VSTS/Azure DevOps – Task – PLEASE SPECIFY THE NAME OF THE TASK
  • Command line – PLEASE SPECIFY THE FULL COMMAND LINE

Issue Description

When the feature.cs file is generated the constructor gets an argument with a generated type [TestNamespace]_XUnitAssemblyFixture which cannot be found and lead to a compile error.
public MyFeature(MyFeature.FixtureData fixtureData, Test_XUnitAssemblyFixture assemblyFixture, Xunit.Abstractions.ITestOutputHelper testOutputHelper)

@SabotageAndi
Copy link
Contributor

Which exact version of SpecFlow are you using?
How is the name of your test project?

@SabotageAndi
Copy link
Contributor

And what is the root namespace of this project?

@xavalon263
Copy link
Author

I use Specflow version 3.1.67.
The name of the test project is: companyname.platform.win32.test.
The root namespace is Compayname.Platform.Test.
The generated type for assemblyFixture is CompanyName_Platform_Test_XUnitAssemblyFixture, which cannot be found.

@Leanwit
Copy link

Leanwit commented Jan 2, 2020

I have the same issue.
I've used the same combination but using VS 16.3.7

The name of the test project is: Mooc
The assembly name is: MoocTest.apps
The root namespace is: MoocTest.apps
The generated type for assemblyFixture is MoocTest_apps_XUnitAssemblyFixture which cannot be found.

@faso
Copy link

faso commented Jan 3, 2020

Same issue here, same parameters as the OP, except we're on .NET Core 3.1

@jjdd12
Copy link

jjdd12 commented Jan 14, 2020

Same Issue, with similar naming

@timmkrause
Copy link

timmkrause commented Jan 15, 2020

Updating to

<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.1.76" />
<PackageReference Include="SpecFlow.xUnit" Version="3.1.76" />

did not solve it either.

Downgraded both to 3.0.225 to fix it.

@timmkrause
Copy link

timmkrause commented Jan 15, 2020

Interesting is also that the "Output" window says

37>Build succeeded.
37>    0 Warning(s)
37>    0 Error(s)
37>
37>Time Elapsed 00:00:25.44
========== Rebuild All: 41 succeeded, 0 failed, 0 skipped ==========

(regardless of the version, same for 3.1.67) and if you filter for "Build Only" in the "Error List" window, it says 0 errors. So it might really be related to IntelliSense in some way...?!

@mootalk
Copy link

mootalk commented Jan 16, 2020

We have the same issue, the tests do run successfully however.

On .net core 3.1
SpecFlow packages are on 3.1.78
IDE is JetBrains rider 2019.3

@jjdd12
Copy link

jjdd12 commented Jan 16, 2020

We have the same issue, the tests do run successfully however.

On .net core 3.1
SpecFlow packages are on 3.1.78
IDE is JetBrains rider 2019.3

I am able to confirm that the test do run successfully, and that this is an IntelliSense only issue for me.

@SabotageAndi
Copy link
Contributor

Ah, now I know what is happening here.

You all have probably R# installed. When you opened the feature.cs file it analyzes it and because it doesn't find the file in the obj folder with the type, you get this error.

@Leanwit
Copy link

Leanwit commented Jan 17, 2020

I didn't install R# on my computer. Never.
It very strange because in my case it happens after update the project from Netcore 2.2 to Netcore 3.1

Hopefully, in my case tests now do run successfully too.

@xavalon263
Copy link
Author

I don't have R# installed.
In my case it happens after updating the specflow nuget package from 3.0.225 to 3.1.67.

@timmkrause
Copy link

timmkrause commented Jan 17, 2020

Also no R# here. Or to be more precise: It is suspended.

@epresi
Copy link
Contributor

epresi commented Jan 27, 2020

Hey guys,

I had a discussion with @SabotageAndi.
The generated assemblyFixture file (e.g.: [TestNamespace]_XunitAssemblyFixture) can be found in the obj folder (obj\Debug\targetframework\xUnit.AssemblyHooks.cs).
We realized that this is IntelliSense related "issue" (IntelliSense of VisualStudio itself, not R#).
As you all noticed, the build was successful, but the error was still there. This is because the generated code behind file (*.feature.cs) is opened in your editor. If you close them, and build again, the error will disappear.
We would like to know, if there is a reason to have the generated code behind file opened?
Maybe there is a use-case which we are not aware of.

BTW there is an ongoing PR which will move the generated files to the obj folder, so this problem will disappear when it'll be merged.

@SabotageAndi
Copy link
Contributor

@timmkrause @xavalon263 @Leanwit @jjdd12 @faso
Any feedback to @epresi comment?
If not, we will close this issue on friday.

@mootalk
Copy link

mootalk commented Jan 29, 2020

The solution does build, but I still get the errors in Rider. I can work with it for now. But it is not sustainable. It's harder to see the actual errors and warnings. How soon do you think the PR will complete?

@SabotageAndi
Copy link
Contributor

@mootalk So you get this errors in Rider without even opening the *.feature.cs files?

@mootalk
Copy link

mootalk commented Jan 29, 2020

Yes, it does compile however and the tests succeed. But if I look at the list of errors and warnings in the solution they do show up

@xavalon263
Copy link
Author

Same here with rider

@caetanopaulo
Copy link

Same here.

IDE: Rider 201903
Specflow: 3.1.74
.net core: 3.1

@stroboscopicus
Copy link

Same here with VS 16.4.4
The error can be reproduced with this example (SpecFlow 3.1.62): https://github.com/SpecFlowOSS/SpecFlow-Examples/tree/master/SpecFlow.json/BowlingKata-SpecFlowJson-xUnit
Downgrading to 3.0.225 solves the problem.

@SabotageAndi
Copy link
Contributor

@epresi had a look at this and we discussed the options.
You get this error displayed in the error list when you open the feature.cs or you have solution wide analyze in Rider/R# enabled.
The reason is that VisualStudio/R#/Rider ignores for this error message the files that are in the obj folder.
It's only a phantom error as at build, the files from the obj folder are included.

We think when we merge PR #1691, which moves all of the feature.cs files in the obj folder, this "phantom" errors should be gone.

It can take some time to get #1691 into master. There are still some merging conflict and problems with the PR.

@stroboscopicus
Copy link

solved by disabling full solution analysis https://docs.microsoft.com/en-us/visualstudio/code-quality/how-to-enable-and-disable-full-solution-analysis-for-managed-code?view=vs-2019

@timmkrause
Copy link

timmkrause commented Feb 10, 2020

Using

<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.1.82" />
<PackageReference Include="SpecFlow.xUnit" Version="3.1.82" />

and VS 2019 16.4.4 without R#.

Cleaned everything (bin/obj), opened the solution (opened files restore disabled, so no file opened on startup) and after a few seconds the errors bubble up. So it is also related to full solution analysis.

But to be honest, disabling full solution analysis is just a temporary workaround for me.
I hope the upcoming PR fixes it, is there an ETA already?

Still stuck on 3.0.225. Additionally this blocks our .NET Core 3.1 migration because of System.AggregateExceptions when running tests with the combination of .NET Core 3.1 and SpecFlow 3.0.225 package versions. So these need to be aligned correctly.

@mootalk
Copy link

mootalk commented Apr 16, 2020

What is the status on this?
It seems to be taking quite long and I'm wondering if it is a better idea to generate the XunitAssemblyFixture into the project itself?
That gives a bit of ease on resolving #1691

@arkiaconsulting
Copy link

I got rid of the errors by adding the following to the specflow test csproj:

<ItemGroup>
    <Compile Remove="*.feature.cs" />
  </ItemGroup>

<ItemGroup>
    <None Include="*.feature.cs" />
</ItemGroup>

@SabotageAndi
Copy link
Contributor

@arkiaconsulting with that you remove the code-behind files from your assembly and so you have no scenarios anymore to execute.

@arkiaconsulting
Copy link

@SabotageAndi after having cleaned, deleted *.feature.cs files, unloaded then reloaded the project, rebuilt, I still have all my scenarii and all my tests pass...

@czarls
Copy link

czarls commented Jan 11, 2021

Hi guys, it's been a while since this bug was raised and there is still no any fox for this, is there?

@SabotageAndi
Copy link
Contributor

@czarls SpecFlow is an Open Source Project that depends on the contributions of its community. We are only a small team and can't do everything.

I am happy to help to get people started working on SpecFlow anytime and review pull requests.
And starting tomorrow, the whole team is available for two weeks to get people started contributing to SpecFlow. Read more about our first OSS Iteration at https://specflow.org/blog/lets-start-the-new-year-working-together-on-specflow/

@godrose
Copy link

godrose commented Feb 16, 2021

@SabotageAndi
I provide here a workaround that I use to overcome this for Resharper part:
You should add *.feature.cs to the list of excluded files: Visual Studio->Extensions->Resharper->Options->Code Inspection->Generated Code->Elements To Skip->File Masks.
Then save it using team-shared settings to make sure problem is solved across the whole team
The resulting line inside the .dotsettings file should look similar to:

<s:Boolean x:Key="/Default/CodeInspection/ExcludedFiles/FileMasksToSkip/=_002A_002Efeature_002Ecs/@EntryIndexedValue">True</s:Boolean>

@godrose
Copy link

godrose commented Feb 16, 2021

@SabotageAndi This solves the problem for Resharper but the Visual Studio still highlights this as an error...
Moreover I tried to use .editorconfig approach and this doesn't help either

@SabotageAndi
Copy link
Contributor

@godrose Does it only display it as error, or are you getting a real compile time error? Is it listed in the output window? What happens if you build on command line?

@godrose
Copy link

godrose commented Feb 16, 2021

@SabotageAndi output window only. The build is fine.
I'm ok with the message but the rest of team is not 🙃

@SabotageAndi
Copy link
Contributor

Are they opening the feature.cs files? I never saw this, when I didn't opened them.

@MatthewLymer
Copy link

I have this exact same problem, the project compiles fine, but the IDE is complaining that myapp_XUnitAssemblyFixture doesn't exist.

A workaround may be to have SpecFlow generate that class as a partial, then we can have a declaration in our solution to satisfy the IDE checks.

@godrose
Copy link

godrose commented Feb 16, 2021

Are they opening the feature.cs files? I never saw this, when I didn't opened them.

Yes they do ))

@godrose
Copy link

godrose commented Feb 17, 2021

@MatthewLymer What do you mean exactly?

@SabotageAndi
Copy link
Contributor

Are they opening the feature.cs files? I never saw this, when I didn't opened them.

Yes they do ))

Why? There is no need for that. All changes will be overwritten with the next build.

@godrose
Copy link

godrose commented Feb 17, 2021 via email

@SabotageAndi
Copy link
Contributor

Understood, and then they are in a loop.
I think the only thing to fix this is to get #1983 finally to work

@MatthewLymer
Copy link

@MatthewLymer What do you mean exactly?

If I have the following code in my integration test assembly:

public partial class MyApp_XUnitAssemblyFixture
{     
}

Then this satisfies the design-time errors, however, when I finally build the solution I get:

xUnit.AssemblyHooks.cs(8, 14): [CS0260] Missing partial modifier on declaration of type 'MyApp_XUnitAssemblyFixture'; another partial declaration of this type exists

Which if the generated class had partial on it, I would get no design-time errors OR any compile time errors, thus working around the problem.

@SabotageAndi
For a lot of IDEs like JetBrains Rider or Visual Studio when using Resharper, you don't even need to open the file before you'll be notified errors in the solution

@godrose
Copy link

godrose commented Feb 17, 2021 via email

@MatthewLymer
Copy link

MatthewLymer commented Mar 22, 2021

If anyone's looking for a good workaround, I think I have one. It works on my company's project that leverages netcoreapp3.1, and I suspect it will work for others too.

there's 2 parts, the first part is to create a partial class for the class that the IDE is saying is not found:

// ENSURE THIS IS NOT IN A NAMESPACE!!!
// ReSharper disable All
public partial class MyCompany_XUnitAssemblyFixture
{
}

It's important that it is NOT in a namespace. Also, obviously update the class name to match your project name's hierarchy, so for my company it ended up being AcmeToonsCorp_IntegrationTests_XUnitAssemblyFixture.

The second part is to update your csproj to include a target that will add the partial keyword to the generated file:

  <Target Name="WorkaroundForSpecFlowXUnitResharperError" AfterTargets="IncludeCodeBehindFilesInProject">
    <ReplaceFileText
            InputFilename="$(IntermediateOutputPath)xUnit.AssemblyHooks.cs"
            OutputFilename="$(IntermediateOutputPath)xUnit.AssemblyHooks.cs"
            MatchExpression="public\sclass"
            ReplacementText="public partial class" />
  </Target>  

  <UsingTask TaskName="ReplaceFileText" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
    <ParameterGroup>
      <InputFilename ParameterType="System.String" Required="true" />
      <OutputFilename ParameterType="System.String" Required="true" />
      <MatchExpression ParameterType="System.String" Required="true" />
      <ReplacementText ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System" />
      <Using Namespace="System.IO" />
      <Using Namespace="System.Text.RegularExpressions" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
            File.WriteAllText(
                OutputFilename, 
                Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText));
          ]]>
      </Code>
    </Task>
  </UsingTask>

I put this immediately after the <PropertyGroup>...</PropertyGroup> node, but the order doesn't seem to be important.

Voila, no IDE errors, and no compile errors, SpecFlow bliss.

Credits for https://stackoverflow.com/a/22571621/507793 for the code snippet to update a text file during msbuild, the snippet I provided has been altered to work with netcore3.1.

FYI I'm running this on Windows 10 with .NET Core SDK 3.1.404

@SabotageAndi
Copy link
Contributor

@MatthewLymer we could make the class partial by default. So if somebody is submitting a PR for it, you would not need the whole second step.

@timmkrause
Copy link

Changing the main code base to allow a workaround. Doesn‘t feel right to me. How about fixing the root cause and eliminating a workaround to get the out of the box experience right for new users?

@SabotageAndi
Copy link
Contributor

@timmkrause The problem is, that it is a lot of work to get the out-of-the-box experience right. If it would be, I would have already fixed it.
The workaround is a quick fix to remove the phantom error message at least for some of you.

The problem here is, that Visual Studio and Resharper aren't looking at all files they should. The compiler is doing it. So we need to work around a wrong behavior of IDEs. And the only workaround we know is a lot of work where we need to be sure that a lot of other stuff (e.g. navigation) is still working.
All this is not really fun to do.

And please don't forget. SpecFlow is an open-source project. It depends on the contributions of its users. We are only a small team with limited time.

So if you want to see this bug go away, jump at #1983 and finish it. And don't wait for Tragedian to do the work for you.
I am happy to jump in a call with someone to explain in detail what is the problem, what needs to be done and what still has to work.

@MatthewLymer
Copy link

MatthewLymer commented Mar 29, 2021

I started making an MR to add the partial and the forward declaration for the MyCompany_XUnitAssemblyFixture class. It's a little more complicated than I was expecting w/ the code generation supporting both C# and VB.net.

My current thinking is 2 parts:

  1. Update the xUnit.AssemblyHooks.template.vb and xUnit.AssemblyHooks.template.cs files to make the class partial.
  2. Update the XUnit2 feature code generation to emit a forward declaration for the MyCompany_XUnitAssemblyFixture class.

@github-actions
Copy link

github-actions bot commented Jul 3, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.