DSP2017, DSP2017 Project

Adding xUnit to my .Net Core app – programming at night sucks!

The last two evenings I reserved for coding. The plan was like this – download all the Polish companies, save them in database and make some functionality in ‘admin area’. But of course first things first – unit tests!

I have to admit, most of the code I write doesn’t have tests. It’s not something I’m proud of but life is life, deadline is deadline – but once again – it’s something that really (really – really!) makes me feel ashamed. So, this time I want to make my pet project properly and write as much unit tests as it’s possible. And I’m really curious if I will stick to this idea till the end of ReMaster. Anyway, forgive me if my tests will be bizarre ;).

Stairway to…

I installed xUnit packages. I wrote a test just for the purpose of checking if everything is fine aaaand… The test didn’t appear in the Test Explorer. After an hour or so I decided that it’s quite late hours for solving such trivial and stupid problems. Let’s use console and start doing something bigger, finally!

So I typed

dotnet test

And received the following:

dotnet-test Error: 0 : System.InvalidOperationException: Can not find runtime target for framework '.NETStandard,Version=v1.6' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'. Possible causes:
1. The project has not been restored or restore failed - run `dotnet restore`
2. The project does not list one of 'win10-x64, win81-x64, win8-x64, win7-x64' in the 'runtimes' section.
3. You may be trying to publish a library, which is not supported. Use `dotnet pack` to distribute libraries.
   at Microsoft.DotNet.ProjectModel.BuildWorkspace.GetRuntimeContext(ProjectContext context, IEnumerable`1 runtimeIdentifiers)
   at Microsoft.DotNet.Tools.Test.TestCommand.<>c__DisplayClass2_0.<DoRun>b__0(ProjectContext c)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.DotNet.Tools.Test.TestCommand.DoRun(String[] args)

So I added

"runtimes": {
"win10-x64": {}
},

to project.json of my test’s project so it’s content looks like this:

{
    “version”: “1.0.0-*”,
    “runtimes”: {
        “win10-x64”: {}
    },
    “dependencies”: {
        “NETStandard.Library”: “1.6.0”,
        “NUnitLite”: “3.6.1”,
        “xunit”: “2.2.0”,
        “xunit.extensibility.execution”: “2.2.0”,
        “xunit.runner.visualstudio”: “2.2.0”
    },
    “frameworks”: {
        “netstandard1.6”: {
        “imports”: “dnxcore50”
        }
    }
}

This time I received even ‘better’ error message:

Project Tests (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
No executable found matching command "dotnet-test-"

Oook, that’s strange…

Because it was long after midnight a great idea came to my mind: “Why the hell I chose xUnit, when I can use NUnit and finally do some meaningful code?!” Yes, I did it – I removed all the xUnit references and added NUnit packages. But hey! Don’t judge me, it was 3:30 AM! Who is responsible at this hour? 😉

In fact I don’t even remember what I was doing in my project with NUnit but you may be ‘surprised’ – in the result, it didn’t show anything in Test Explorer.

This way, I started feeling something close to hate to my pet project so I was able to come back to the topic… two days later ;).

I started back with xUnit. And in some magical way I found that I need dotnet-test-xunit package. And that my project.json lacks

"testRunner": "xunit"

section! Whoah! So I make some changes to the file and it looks like this now:

{
    "version": "1.0.0-*",
    "testRunner": "xunit",
    "dependencies": {
        "NETStandard.Library": "1.6.0",
        "NUnitLite": "3.6.1",
        "xunit": "2.2.0",
        "xunit.extensibility.execution": "2.2.0",
        "xunit.runner.visualstudio": "2.2.0",
        "dotnet-test-xunit": "2.2.0-preview2-build1029"
    },
    "frameworks": {
        "net461": {
            "Microsoft.NETCore.App": "1.0.0-rc2-3002702"
        }
    }
}

And you now what? My test appeared in the Test Explorer! Of course I can also run it from the console. Wow! I can finally type some code! That’s incredible! Night coding without possibility of drinking coffee (yes! It’s my poor fate – living without caffeine for more than a year now!) really sucks!

And what are yours night adventures with coding?

Featured image by Robert Bock

Share this: