JUnit 5 Console does not find tests - unit-testing

I am using JUnit5 Console in my Ant build script. However it does not find any tests in my project.
I've used the following command in Terminal:
-jar lib\junit-platform-console-standalone-1.1.0-RC1.jar --class-path bin --scan-class-path
which returns:
[36m.[0m
[36m+--[0m [36mJUnit Jupiter[0m [32m[OK][0m
[36m'--[0m [36mJUnit Vintage[0m [32m[OK][0m
Test run finished after 27 ms
[ 2 containers found ]
[ 0 containers skipped ]
[ 2 containers started ]
[ 0 containers aborted ]
[ 2 containers successful ]
[ 0 containers failed ]
[ 0 tests found ]
[ 0 tests skipped ]
[ 0 tests started ]
[ 0 tests aborted ]
[ 0 tests successful ]
[ 0 tests failed ]
I get the same output for the Ant script:
<target name="test" depends="compile" description="Runs JUnit Tests">
<java jar="lib\junit-platform-console-standalone-1.1.0-RC1.jar" fork="true">
<arg value="d ."/>
<arg value="-details verbose"/>
</java>
</target>

I fixed it: The Arguments have to be lines:
<target name="test" depends="test-javac" description="Runs JUnit Tests">
<java jar="${junit.jar}" fork="true">
<arg line="--class-path bin"/>
<arg line="--scan-class-path"/>
<arg line="--reports-dir reports"/>
<!-- Available options: [ascii,unicode]-->
<arg line="details-theme unicode"/>
<!-- Available options: [none,flat,tree,verbose]-->
<arg line="--details tree"/>
</java>
</target>

You need to specify where the launcher can find your compiled test classes. See https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher for details.
How are your test classes named? Are they stored by "javac" directly under "bin/"?
Maybe the answer and comments posted here Unable to run tests with JUnit5 Console Launcher are helpful.

Related

Visual Studio 2017 csproj .NET CORE netstandard2.0 not building

I just installed VS2017 and did the one way migration of my .NET core projects from the project.json format to the new csproj format. What I want is to target multiple frameworks so I can build a Framework Depedenent Deployment and a Self Contained Deployment using a smaller footprint. I followed the directions on the MS docs, but when I include netstandard1.6 or netstandard2.0 in the TargetFrameworks, I get a whole slew of Predefined type System.Object is not defined and The type of namespace System could not be found among others when I try and build the project. This worked when it was using the project.json file. My csproj is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>1.0.0.0</VersionPrefix>
<TargetFrameworks>netcoreapp1.2;netstandard2.0</TargetFrameworks>
<AssemblyName>App</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>App</PackageId>
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.2' ">1.1.1</RuntimeFrameworkVersion>-->
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard2.0' ">1.6.1</NetStandardImplicitPackageVersion>
<RuntimeIdentifiers>win10-x64;android.21;android21-arm64;osx.10.12;rhel7.4;centos.7-x64;debian8-x64;ubuntu16.10-x64;fedora.26-x64;opensuse.42.1-x64</RuntimeIdentifiers>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute
</PropertyGroup>
<ItemGroup>
<None Remove="App.csproj.vspscc" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
<PackageReference Include="Microsoft.NETCore.Runtime.CoreCLR" Version="1.1.0" />
</ItemGroup>
</Project>
My original project.json
{
"version": "1.0.0.0",
"buildOptions": {
"emitEntryPoint": true
},
"frameworks": {
"netcoreapp1.2": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
}
},
"netstandard2.0": {
"dependencies": {
"NETStandard.Library": {
"version": "1.6.1"
},
"System.Threading.Thread": "4.3.0",
"Microsoft.NETCore.Runtime.CoreCLR": "1.1.0"
}
}
},
"runtimes": {
"win10-x64": {}
"ubuntu.16.10-x64": {},
"centos.7-x64": {},
"debian.8-x64": {},
"fedora.24-x64": {},
"opensuse.42.1-x64": {},
"osx10.12-x64" : {}
}
}
Not sure what the problem is. Am I trying to do something unsupported? If I have just netcoreapp1.2, when I do a dotnet publish -c Release -r win10-x64 I still get a FDD output, not a standalone executable. I feel like this was way easier with the json file... What am I doing wrong?
I got the same error message when the Nuget-packages were not restored. Have you made sure the packages are restored properly, and that no errors appear if you run "dotnet restore"?

Is there a way to get build status as a property?

I have a ugly Teamcity build configuration using MSBuild. It executes custom application (test runner), which is using custom messaging to report test results to teamcity.
##teamcity[testStarted name='test1']
##teamcity[testFailed name='test1' message='failure message' details='message and stack trace']
Which show in teamcity in build overview and tests tab.
Teamcity recognizes failed tests and if any test fails, it marks the build as failed:
http://i.stack.imgur.com/Qz9UT.png
Later in the MSBuild target I would like to label cvs based on the test results.
Is there a way to get the build status (if it is failed, hanging, warning) as a property? something like %build.status%? The format does not matter - if its a string or number.
PS: I know that best solution to my problem would be to modify the application to return non-zero exit code if test fail.
TeamCty does not seem to expose this directly, but the status can be acquired using the REST api. Here is an example using curl; but you could also uwe PowserShell's Invoke-RestMethod for instance.
Here's the msbuild script that casues test failure I used for testing:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Test">
<Message Importance="high" Text="##teamcity[testStarted name='test1']" />
<Message Importance="high" Text="##teamcity[testFailed name='test1' message='failure message' details='message and stack trace']" />
</Target>
</Project>
Then the script that gets the current build's status, dumps it to a file, reads the file into an msbuild item and then uses regex to get the status out of it. You just have it to supply the tc_user and tc_password properties (or allow guest access) and change the url to match your server.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="GetBuildStatus">
<Target Name="RunCurl">
<PropertyGroup>
<MyTempFile>curl_out</MyTempFile>
</PropertyGroup>
<Exec Command="curl http://localhost/httpAuth/app/rest/builds/id:$(teamcity_build_id) -basic -u $(tc_user):$(tc_password) > $(MyTempFile)"/>
<ReadLinesFromFile File="$(MyTempFile)">
<Output TaskParameter="Lines" ItemName="CurlOutput"/>
</ReadLinesFromFile>
<Delete Files="$(MyTempFile)"/>
</Target>
<Target Name="GetBuildStatus" DependsOnTargets="RunCurl">
<PropertyGroup>
<CurlOutputFull>#(CurlOutput)</CurlOutputFull>
<BuildStatus>$([System.Text.RegularExpressions.Regex]::Match($(CurlOutputFull), `status="(\w*)"`).Groups[ 1 ].Value)</BuildStatus>
</PropertyGroup>
<Message Text="BuildStatus = $(BuildStatus)"/>
</Target>
</Project>
This prints:
BuildStatus = FAILURE

ant: create dirset with all dirs containing files in a fileset

Context: a directory, recursively containing python unittest.TestCases:
projectdir/build.xml
projectdir/src/x.java
projectdir/distribute/p.jar
projectdir/tests/a/test1.py
projectdir/tests/a/test2.py
projectdir/tests/b/test1.py
projectdir/tests/c/test1.py
projectdir/tests/javaunittests/test2.java
And I want ant to call
python -m unittest discover -s a b c
Now I was trying to convert a fileset to the dirset of the containing directories? The idea is to run python unittests:
<apply command="python">
<arg line="-m unittest"/>
<arg value="-s"/>
<dirset include="${python_unittests}"/>
</apply>
where ${python_unittests}" would refer to afileset` (but I don't find how) like this:
<fileset id="python_unittests" include="**/*.py">
<containsregexp expression="\(unittest.TestCase\)"/>
</fileset>
Or am I erring, and is there a better way to achieve this?
You want all python files with testcases, you don't need a dirset, use :
<apply executable="python">
<arg line="-m unittest"/>
<arg value="-s"/>
<fileset dir="projectdir" includes="**/*.py">
<contains text="unittest.TestCase"/>
</fileset>
</apply>
EDIT
Just checked the docs for unittest on python.org after your comment.
As i understand it, that should be sufficient :
<property name="projectroot" location="foo/bar/yourprojectdir"/>
<exec executable="python" failonerror="true">
<arg line="-m unittest discover ${projectroot} 'test*.py'"/>
</exec>
as python docs section 26.3.3. Test Discovery explains :
The -s, -p, and -t options can be passed in as positional arguments in that order. The following two command lines are equivalent:
python -m unittest discover -s project_directory -p '*_test.py'<br>
python -m unittest discover project_directory '*_test.py'
I expect that discover works recursively starting from project_directory.

Clean up after nant build failures

I'm looking for my nant build script to be able to clean up after itself if a build goes wrong. I'm looking for something resembling the following execution:
Target= Software.Build
Target= Software.Build.Success *(depends on Software.Build succeeding)*
Target= Software.Build.Failed
I am looking for a solution that if the Software.Build target fails then Software.Build.Failed will be executed e.g. to e-mail someone that the build failed in some way, otherwise Software.Build.Success will be run to allow the build script to continue.
Is this even possible with nant? If so, could anyone point me to a suitable article/solution?
Or if you have global data to be cleaned up, you can use the NAnt OnFailure event.
<property name="nant.onfailure" value="failure" />
<target name="failure">
<!-- Put your cleaning code in here -->
</target>
NAntContrib has a trycatch task:
<trycatch>
<try>
<call target="Software.Build" />
</try>
<catch>
<call target="Software.Build.Failed" />
<fail message="build failed" />
</catch>
<finally>
<!-- execute everything that doesn't depend on success or failure -->
</finally>
</trycatch>
<call target="Software.Build.Success" />

nant script doesn't display unit test details

Can someone please tell me why my build script (nant) doesn't display the unit test details in the command prompt window? I have verbose set to true, but it doesn't want to display any details about my unit tests. Here's the target:
<target name="run-unit-tests" depends="compile, move.assemblies.for.tests, rebuildDatabase">
<mkdir dir="${tests.output.dir}" />
<nunit2 haltonfailure="true" failonerror="true" verbose="true">
<formatter type="Xml" extension=".xml" outputdir="${tests.output.dir}" usefile="true" />
<test assemblyname="${test.assembly.file}" />
</nunit2>
<echo message="Unit Testing Done!" />
</target>
The command prompt window just displays this:
[mkdir] Creating directory 'C:\Projects\TestProject\build\artifacts\UnitTestOutput'.
[echo] Unit Testing Done!
build:
BUILD SUCCEEDED
Am I missing something here?
Thanks!
I found the answer. I looked at the source for CodeCampServer and saw a line
<formatter type="Plain" />
and added it to my build script so it looks like this:
<nunit2 haltonfailure="true" failonerror="true" verbose="true">
<formatter type="Xml" extension=".xml" outputdir="${tests.output.dir}" usefile="true" />
<formatter type="Plain" />
<test assemblyname="${test.assembly.file}" />
</nunit2>
and now it displays the details.
Sorry to ask the question prematurely on here, but at least it might help someone in the future if they have a similar problem.
Is there a log file in ${tests.output.dir} ? If so, what if you set usefile to false and type to "Plain"?