Gradle groovy plugin's test is not picking up test resources - unit-testing

Probably a stupid question.
I'm writing a unit test for my gradle plugin. I understand that gradle custom plugin has groovy plugin applied by default, so plugin/src/test/resources will be included by default as my test resources.
Facts:
Test class location: plugin/src/test/groovy/foo/bar/Test.groovy
Test resource location: plugin/src/test/resources/foo/bar/myfile
I'm trying to access myfile from Test.groovy via
new File(this.class.getResource(".").toURI())
When debugging the above code resolves to this directory and myfile is not in there.
plugin/build/classes/test/foo/bar
myfile can however be found at
plugin/build/resources/test/foo/bar/myfile
Question: How do I access myfile from Test.groovy? Is this a standard behavior from groovy plugin?
UPDATE
What I wanted to get in my original use case was the directory because I have multiple resources (it was lost in the translation to this SO question). It seems like the resource is resolved correctly when I specify myfile like what Peter says. So:
new File(this.class.getResource(".").toURI()) --> plugin/build/classes/test/foo/bar
new File(this.class.getResource("myfile").toURI()) --> plugin/build/resources/test/foo/bar/myfile
I was adopting this hack before finally fixing it with explicit resource names: https://code.google.com/p/android/issues/detail?id=64887#c13

Instead of ., use myfile. If that doesn't help, try getClass().classLoader.getResource("foo/bar/myfile").

Related

Django Management Command best practice for additional file location

I am writing a Django management command. The command itself is located under myapp/management/commands/mycommand.py. I need to write an additional class which I would like to place in an extra file.
Should this file live in
myapp/extrafile.py or
myapp/management/commands/extrafile.py
What would be a recommended location. The class is only need for the management command, not elsewhere in the app.
You can create an additional file in myapp/management/commands/ that starts with underscore. It is not going to be detected as a management command (check this).
So, your command in: myapp/management/commands/mycommand.py
And your helper in: myapp/management/commands/_myhelper.py
Generally in these cases you are writing a helper file for that file so You can create a helper folder like this:
myapp
-helpers
--commands
--other_helpers
you can read more about helpers here, they can be separated from the original class/function.

Testbox 2.1 - Skip an entire directory or CFC file

We want to create a HelperUtility.cfc with common methods for our tests to use. If we put the file in /tests/lib/HelperUtility.cfc, can we tell TestBox, don't try running any tests in /tests/lib? If not, can we add something to the component tag to skip the entire file, rather than adding skip to all the methods in the component individually?
There's no way to do that unfortunately.
I have tried to skip some manual mocks that were created inside a tests/mock folder, but you cannot configure TestBox at runtime to skip a specific folder if you decide to run the tests for a parent folder.
The only work around that worked for me was to create a specs subfolder in the parent tests and then call the testbox runner with a directory argument of the specs...
For example: http://localhost:8500/testbox/system/runners/HTMLRunner.cfm?directory=tests.specs

Set screenshot path from default project location to different folder location

I have a suite which has 50 test cases. When I execute my suite, I get all the failed screenshots listed in the project's folder. I want to point and store those screenshots to a different directory with the name of the test case. I wanted it to be a one time setup than doing it explicitly for every test cases.
There's quite a few ways to change the screenshots default directory.
One way is to set the screenshot_root_directory argument when importing Selenium2Library. See the importing section of Selenium2Library's documentation, and importing libraries in the user guide.
Another way is to use the Set Screenshot Directory keyword, which will do pretty much the same thing as specifying a path when importing the library. Though, using this keyword you can set the path to a new one whenever you like. For example, you could make it so that each test case could have it's own screenshot directory using this keyword. According to your question, this may be the best solution.
And finally, you may also post-process screenshots using an external tool, or even a listener, that would move all screenshots to another directory. Previously mentioned solutions are in most cases much better, but you still may want to do this in some cases, where say, the directory where you want screenshots to be saved would be created only after the tests have finished executing.
I suggest you to do the follow:
For new directory, you should put the following immediately after where you open a browser such:
Open Browser ${URL} chrome
Set screenshot directory ${OUTPUT FILE}${/}..${/}${TEST_NAME}${/}
For replace the screenshot name from the default to your own name, create the following keyword:
sc
Capture page screenshot filename=${SUITE_NAME}-{index}.png
Then, create another keyword and run it on Setup's test case:
Register Keyword To Run On Failure sc
In the above example, I created a new folder with the test case name, which create a screenshot (in case of failure) with the name of suite project name (instead of 'selenium-screenshot-1.png').

Jenkins Groovy to extract Regex from the Current build log and call REST API?

I would like to add a build step that summarizes my build, using Groovy to read the build log that was generated to date.
I've seen several other questions on SO about related topics but not all of them run, I'm a bit confused on the API docs, and overall I can't seem to get this exact thing running.
Below is the code/resultant failure I have currently.
I have a few questions, if it is ok to put them all together here;
1.Is it safe to test things in the console window? Or, stated differently, when can it be that something works in the /script Groovy console editor window, but it will fail as a Groovy build step? (I think the API differs for the two but I'm not clear how.)
2.Is there a repo anywhere of Groovy Jenkins script examples?
3.How can I do the following?
Read the console log.
Parse it with regex for words of interest, eg "step#2 success".
Rearrange those words into a nice string with some newlines.
Call our internal REST API to submit the results.
thank you so much!
Anne
//Groovy command from SO Post#23139654
def log = manager.build.logFile.text
def summary = log =~ /(?ms)(TEST SUMMARY.*?failures)/
//From there you can extract the matches or as in my case further parse the match:
def total = summary[0] =~ /\d+ tests/
Result includes;
ERROR: Build step failed with exception
groovy.lang.MissingPropertyException: No such property: manager for class: Script1
Here are my answers.
1.Groovy console vs Groovy build step differ as per Jenkins Packages on Groovy Classpath?
2.Examples are available from a user in 2011 and unidentified dates on the wiki: "Jenkins, Groovy System scripts (and Maven) | Code Snippets" https://mriet.wordpress.com/2011/06/23/groovy-jenkins-system-script/
and https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console
3.To parse the console log and grep outputs , simply input into the web box provided as input for the Editable Email plugin [4] post-build step.
Do NOT use dollar-curlyBrace syntax: use simple dollar-variable or dollar-paren syntax as shown here which is my first crack at the 'Default Content'.
STATUS=$BUILD_STATUS
$DEFAULT_CONTENT
GIT Changelog , Revision = $GIT_REVISION
$CHANGES
LOG-SNIPPETS: Regex Hits/Rules for words that give Unit Test Summaries, Error, Failure, etc =
$BUILD_LOG_REGEX( regex="^.*?BUILD FAILED.*?$", linesBefore=0, linesAfter=10, maxMatches=5, showTruncatedLines=false, escapeHtml=true)
3B.To call the REST plugin, that would need to be done in a separate step, so for now I did not do that.
I had not properly understood the Email-Ext (aka "Editable Email Notification") plugin - that is why I was trying to do this directly in Groovy.
4.[] Email-ext plugin - Jenkins - Jenkins Wiki ; ; https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin

How to use FireUnit to test my web page?

Can someone please tell me how can i test my web page in FireUnit?
http://ejohn.org/blog/fireunit/
I just played around with it last week and wrote a few test cases. You'll want to create a test case file and include it as part of your page. (perhaps use a conditional like "debug=true" to
prevent inclusion during production)
I found the .Compare method to be the most useful. You can write stuff like:
fireunit.compare(
"expected result",
callToFunction2(), // tested
"This is the test name"
);
Note that since you are comparing strings here, if you want to do JSON instead, you'll have to JSON.stringify the output first.
Upon running the page with the included js file, you'll see test results under the "Test" panel of firebug. (assuming you have the extension installed)