Ant junit target to run tests having certain pattern? - junit

I want to my ant junit target to accept a String value from user. This String value will basically be the package name .The junit target should run the test present under this package and not any other test. How do i do the same. Currently , my junit target looks like this:
<target name="junit" if="run.junit">
<junit dir="${project.root}" fork="true">
<classpath>
<path refid="junit.classpath"/>
</classpath>
<batchtest >
<fileset dir="test">
</fileset>
</batchtest>
</junit>
</target>
How do i modify this to run the tests only as per the given input?

In the fileset resources, add an include pattern with the package criteria, e.g.:
<junit dir="${project.root}" fork="true">
<classpath>
<path refid="junit.classpath"/>
</classpath>
<batchtest >
<fileset dir="test">
<include name="my/package/**"/>
</fileset>
</batchtest>
</junit>
See the documentation for more examples.

Related

ant task to run JUnit tests from a jar (but only files that have tests!)

We're using JUnit inside a custom framework to test an applications behaviour. We're not actually doing unit testing, just leveraging JUnit.
I've created an ant task to run all the tests in the jar file, but unfortunately it's trying to run everything as a JUnit test. Since the jar file contains things besides just the tests (it contains the supporting framework) this is a problem.
Is there a way to make the junit task only run things marked as tests (we use #Test)?
Currently my ant task looks like this:
<target name="test">
<junit printsummary="yes" haltonfailure="no">
<classpath refid="library.third-party.classpath" />
<classpath>
<pathelement location="${basedir}/build/jar/fidTester.jar" />
</classpath>
<formatter type="plain" />
<formatter type="xml" />
<batchtest fork="no" todir="${basedir}/reports">
<zipfileset src="${basedir}/build/jar/fidTester.jar" includes="**/tests/**/*.class" />
</batchtest>
</junit>
</target>
From the Ant JUnit Task documentation:
skipNonTests
Do not pass any classes that do not contain JUnit tests to the test
runner. This prevents non tests from appearing as test errors in test
results. Tests are identified by looking for the #Test annotation on
any methods in concrete classes that don't extend
junit.framework.TestCase, or for public/protected methods with names
starting with test in concrete classes that extend
junit.framework.TestCase. Classes marked with the JUnit 4
org.junit.runner.RunWith or org.junit.runner.Suite.SuiteClasses
annotations are also passed to JUnit for execution, as is any class
with a public/protected no-argument suite() method.

Sonar jacoco coverage without running junit

I've following setup in my build.xml for running jacoco:
<formatter type="xml" />
<batchtest todir="${reports.junit.xml.dir}">
<fileset dir="${test.dir}">
<include name="**/*.java" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
But when I run this it's giving me :
[junit] Test FAILED
Now developers are working on fixing the junits, but I need to know if "without" running junits can I still show how much is the unit test coverage in sonar ?
To answer your question, no you can't get coverage data without running the unit tests. However, you can get coverage data even if the unit tests fail. You just need to keep unit test failure from failing the build & thereby preempting the output of the coverage report.
It looks like the default value of the <junit> tag attribute haltonfailure is off. So either remove your on or turn it off explicitly.

Propagate=False in BIML

I'm using BIML to create my package.
There is a sequence container where I want to set the system variable Propagate to False.
I cannot find the method in BIML to set the system variable for Propagate.
Propagate is a System scoped variable that only exists within Event Handlers.
The following Biml creates an SSIS Package with an OnError event handler that has an empty Sequence Container
The Event itself redefines the System::Propagate variable to have a value of false instead of the default value of true
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<Package Name="so_38121956">
<Events>
<Event EventType="OnError" Name="OnError">
<Variables>
<Variable DataType="Boolean" Name="Propagate" Namespace="System">false</Variable>
</Variables>
<Tasks>
<Container Name="SEQC Propagate no more">
</Container>
</Tasks>
</Event>
</Events>
</Package>
</Packages>
</Biml>
Try using BIML Online.
http://bimlonline.com/
Here you can upload an existing SSIS package with propagate set and the browser based application will reverse engineer it for you to give you the BIML.
Its handy if you know how to do something in SSIS, but not sure of the BIML equivalent.
Officially its still in Beta, but works well.

How can I configure logback conditionally based on context name?

I have three separate projects, each with their own embedded logback.xml files. Each of these files includes a common logging config file in the user's home durectory:
<include file="${user_home}/loggingConfig.xml"/>
After the include, I have this specification:
<root level="error">
<appender-ref ref="${appender:-console}" />
</root>
This allows the user to configure their log levels and appenders, and have them applied by the core logging config file.
For example, in ~/loggingConfig.xml I have this line:
<property name="appender" value="file" />
But co-workers who prefer console logging leave that line out.
The problem is I would like to use different appenders for each log file. In otherwords, I would like to conditionally set a different appender based on which project is reading the customized config file.
I realize I could configure each project to read differently named config files, but I would like to eliminate the clutter and allow for shared configuration as well.
The documentation is a bit spare for advanced configuration, but I found that you can use the logback context name as a variable with conditional logging. So for each project I define a custom context name in the projects logback.xml file:
<contextName>project1</contextName>
etc...
Then in my ~/loggingConfig.xml file I can do this:
<property name="appender" value="file" />
<!--if condition='property("CONTEXT_NAME").equalsIgnoreCase("project1")'>
<then>
<property name="appender" value="file" />
</then>
</if-->
<if condition='property("CONTEXT_NAME").equalsIgnoreCase("project2")'>
<then>
<property name="appender" value="console" />
</then>
</if>
<if condition='property("CONTEXT_NAME").equalsIgnoreCase("project3")'>
<then>
<property name="appender" value="file" />
</then>
</if>
This can get a bit clunky, but in reality I am using this solution to configure properties used by a single appender for different projects, while still having a graceful fallback to a default value for projects that don't have their own conditional block.
In case this helps anyone else, this is how I set up conditional logback configuration with a property that can contain multiple appenders:
<root level="${logback.loglevel}">
<if condition='isDefined("logback.appenders")'>
<then>
<if condition='property("logback.appenders").contains("CONSOLE")'>
<then>
<appender-ref ref="CONSOLE"/>
</then>
</if>
<if condition='property("logback.appenders").contains("FILE")'>
<then>
<appender-ref ref="FILE"/>
</then>
</if>
<if condition='property("logback.appenders").contains("GELF")'>
<then>
<appender-ref ref="GELF"/>
</then>
</if>
</then>
<else>
<appender-ref ref="CONSOLE"/>
</else>
</if>
</root>

Read Config Value in MSBuild Task

Is there a way to read a System.Config connection string in an MSBuild task?
Basically I have my connection string setup in a config file
<add name="MyApp.MyConnectionString" connectionString="..." />
And I would like to reference it in an MSBuild task like so ...
<Target Name="Migrate" DependsOnTargets="Build">
...
<Migrate Connectionstring="$(MyApp.MyConnectionString)" ... />
</Target>
There's an XMLRead task in the MSBuild Community Tasks Project, that uses xpath to pull out a value.
<XmlRead
XPath="/add/#connectionString"
XmlFileName="app.config">
<Output TaskParameter="Value" PropertyName="MyConnectionString" />
</XmlRead>
<Message Text="MyConnectionString: $(MyConnectionString)"/>
(note: totally untested)