Setup each Scalatest "Should" block with specific code - scalatest

I want to setup each "should" block in my suite with a different input file.
After which, each "in" block will use it's own should block file.
I'm familiar with beforeAll and with beforeEach but they don't help me since beforeAll runs once before all tests and beforeEach runs the same code before each test where I want to run different code for each test.
I tried writing my setup code inside each "should" block but this way all the "should" setup code runs first before the "in" blocks.
The following is what I tried and also explains what I need:
"ParquetFormatParser with input file 1" should {
setupFile1()
"do something 1" in {
useFile1()
}
"do something 2" in {
useFile1()
} }
"ParquetFormatParser with input file 2" should {
setupFile2()
"do something 1" in {
useFile2()
}
"do something 2" in {
useFile2()
}
}
thanks

Not sure if it will work for you base on your comment about beforeEach but you can use fixtures for setting up the files. This way, files are only setup when needed and tests are independent of each other.
trait FileOneFixture {
// setup file here
}
"ParquetFormatParser with input file 1" should {
"do something 1" in new FileOneFixture {
useFile1()
}
"do something 2" in new FileOneFixture {
useFile1()
}
}

Related

Jenkins Pipeline: How to access pipeline parameters and insert them into batch file argument

I am trying to use the parameterized build and I am getting lost in some sources right now. I thought it would be possible to access the build paramters with a "params.myParameter" call. Or a ${env.myParamter} but neither appear to be working. This does not cause syntax errors currently, but the parameter is being read as "" for the if statement and the param access is being used as a string for the batch call.
What I have is the following:
pipeline {
agent {
node {
label 'dummy_label'
}
}
options {
skipDefaultCheckout true
}
stages {
stage('Setup') {
steps {
// For debug purposes, state which user we are running as
bat 'whoami'
// Now do the checkout
timeout(time: 60, unit: 'MINUTES') {
retry(10) {
// Sometimes this fails because Integrity is terrible.
checkout scm
sleep 5
// Check to see if "VerifyCheckout.bat" exists. Sometimes Integrity doesn't resync the files... just the folders. Again, Integrity is terrible.
bat ".\\HWIOAPPL\\Test\\Jenkins_scripts\\VerifyCheckout.bat"
}
}
dir("${env.PROJECT_ROOT}\\HWIOAPPL\\Test\\Jenkins_scripts") {
bat ".\\QAC_CLI_Setup.bat"
script{
if(params.Release_Tag != "mainline"){
bat ".\\ZIP_Software.bat 'params.Release_Tag'.zip"
}
}
}
}
} //More stages and stuff after this
You need to specify your parameters in a parameters block at the top of the file - between the options and stages block should do (https://www.jenkins.io/doc/book/pipeline/syntax/#parameters).
ex.
parameters {
string(name: 'Release_Tag', defaultValue: '1.0', description: 'Version of the Release')
}

End Gatling simulation when scenario fails BUT generate a report

I have code which currently will not run my scenario if it fails;
//Defined outside of the scenario scope
var simulationHealthy = true
//defined within the scenario
.exec((session: io.gatling.core.session.Session) => {
if (session.status == KO) {
simulationHealthy = false
}
session
})
However my simulation keeps running until the duration set for the simulation is over, though the scenario will not keep executing.
What I would like to do is to have a scenario fail under conditions I define (similar to assertions) and for the entire simulation to fail at that point as well, and also generate a report.
Thanks
Edit: I am running these tests within the IntelliJ IDE. Ending the simulation programmatically is required.
You might run the test itself without report and produce the report with a second call for just the report generation from the simulation.log
Run Simulation w/o report (-nr flag), i.e.
gatling.sh -nr -s YourSimulationClass
Generate Report (-ro flag):
gatling.sh -ro yoursimulation
(your simultation is the path underneath the results folder, which can be specified with -rf, which contains the simulation.log file)
In IntelliJ you can define another LaunchConfiguration to be executed before. So you define an action for executing Gatling Test (with -nr flag) and another configuration for report generation (with -ro flag), that executes the Gatling Test run action before.
Alternatively you could use the gatling-maven-plugin and define two executions (run, report) with the same flags.
Edit
According to this group thread you could execute your steps conditionally or mute them. The condition could be the presence of an error, but anything else as well. If the condition depends on global state i.e. a global variable, it would mute all users (unlike exitHereIfFailed)
For example:
val continue = new AtomicBoolean(true)
val scn = scenario("MyTest")
.exec(
doIf(session => continue.get) {
exec(http("request_0").get("/home").check(status.is(200)))
.exec((session: io.gatling.core.session.Session) => {
if (session.status == KO) {
continue.set(false)
}
session
})
})
As said, this only stops sending requests to the SUT. Seems there is no other option at the moment (apart from System.exit(0))
You can use exitHereIfFailed in ScenarioBuilder returned by exec().
.exec(http("login")
.post("/serviceapp/api/auth/login")
...
.check(status.is(200))))
.exitHereIfFailed
.pause(1)
.exec(http("getProfileDetails")
.get("/serviceapp/api/user/get_profile")
.headers(authHeader("${token}"))
.check(status.is(200)))
Thanks to #GeraldMücke 's suggestion of using system.exit I've come up with a work around. Still no where close to ideal but it does the job.
The problems are
Still have to manually generate the report from the log that is created when gatling is run
The user has to constantly manage how long the scenario lasts for both items as I don't know a way to have a scenario last the length of a simulation
This is obviously a "proof of concept" it has nothing in the code to define failure over thresholds etc like the asserts and checks available in Gatling itself
Here's the code. I've nested simulations within the setUp function because it fits the criteria of the work I am doing currently, allowing me to run multiple simulations within the main simulation.
FailOverSimulation and ScenarioFailOver are the classes that need to be added to the list; obviously this only adds value when you are running something that loops within the setUp.
import java.util.concurrent.atomic.AtomicBoolean
import io.gatling.commons.stats.KO
import io.gatling.core.Predef._
import io.gatling.core.scenario.Simulation
import io.gatling.http.Predef._
import scala.concurrent.duration._
object ScenarioTest {
val get = scenario("Test Scenario")
.exec(http("Test Scenario")
.get("https://.co.uk/")
)
.exec((session: io.gatling.core.session.Session) => {
if(session.status == KO) {
ScenarioFailOver.exitFlag.set(true)
}
session
})
}
object TestSimulation {
val fullScenario = List(
ScenarioTest.get.inject(constantUsersPerSec(1).during(10.seconds))
)
}
object ScenarioFailOver {
var exitFlag = new AtomicBoolean(false)
val get = scenario("Fail Over")
.doIf(session => exitFlag.get()) {
exec(s => {
java.lang.System.exit(0)
s
})
}
}
object FailOverSimulation {
val fullScenario = List(
ScenarioFailOver.get.inject(constantUsersPerSec(1).during(10.seconds))
)
}
class SimulateTestEnding extends Simulation {
setUp(
FailOverSimulation.fullScenario
::: TestSimulation.fullScenario
).protocols(
)
}

How to test a loop inside an AngularJS service

I'm trying to testing service but i don't know how to test loop inside. Is it possible to test case with host.lenght===1 in different way than mock url that return host.length=1. Do I have to callFake all my code inside service to test it? Also how to test isNaN?
Here's my code:
if (host.length === 1 || !isNaN(host[host.length - 1])) {
name = a.hostname;
} else {
if (host[0] === "www") {
slice = 1;
}
name = host.slice(slice, host.length - 1).reverse().join(" ");
}
jsfiddle Demo
In this case, it looks simplest to just use a URL that will give you this behaviour. In my opinion, this will also be a better test as it will actually test the intent of the code, rather than just ensuring that certain methods are called. After all, this code was written to cope with different types of URL, why not test them with exactly that?
You already have a test for this url: 'http://angular.com/about'. For the case you are talking about, you could use 'http://angular/about' and then 'http://www.angular.com/about' to test the code that strips out the www. part.

How to exit a testcase in protractor if element contains value

I've been writing a suite of tests in Protractor, and am almost finished. I'm having difficulty figuring out how to do something fairly common though. I want to be able to check if a textbox contains a value, and if so, exit the testcase with a failure. (If the textbox contains a value, I know there's no chance the test case can pass anyway)
I'm currently trying something like this:
tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
//attr contains the correct value in the textbox here but how do I return it to parent scope so I can exit the test case?
console.log("attr length is " + attr.length);
expect(attr.length).toBe(0);
},function(err){
console.log("An error was caught while evaluating Date of Birth text value: " + err);
});
The expect statement fails as I'd expect it to, but the testcase keeps going, which seems to be the intended behavior of expect. So I tried returning a true/false from within the 'then' block, but I can't seem to figure out how to return that value to the parent scope to make a determination on. In other words, if I change the above to:
var trueFalse = tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
if(attr === ""){
return true;
}else{
return false;
}
},function(err){
console.log("An error was caught while evaluating Date of Birth text value: " + err);
});
//This is the 'it' block's scope
if(trueFalse == true){
return;
}
I know my inexperience with promises is probably to blame for this trouble. Basically, I just want a way of saying, 'if such and such textbox contains text, stop the test case with a failure'.
Thanks,
This is not a protractor issue, it is a testing framework issue (jasmine, mocha etc).
Though, there was an issue on protractor's issue tracker:
--fail-fast CLI option
which was closed with a reference to an opened issue in jasmine issue tracker:
Feature Request - fail-fast option
While this is not implemented, there is a workaround (originally mentioned here):
jasmine-bail-fast package
After installing the module with npm, when you want your test to fail and exit, add:
jasmine.getEnv().bailFast();
(don't forget to load the dependency require('jasmine-bail-fast');)
Also see:
Does jasmine-node offer any type of "fail fast" option?
The issue with the code above is that you are trying to compare a 'Promise' and a 'value'.
trueFalse.then(function(value)
{
if(value)
....
});
OR compare it via an expect statement. Something like expect(trueFalse).toBe(false);

Unable to access NameSpace.app variables with ST2?

I'm using the Sencha Command Line 3 tools with a newly generated Sencha Touch 2 application.
Assuming my app.js file looks like this:
Ext.application({
name: "CA",
event_code: "test123",
launch: function() {
console.log("application launched!");
}
});
My views and object stores depend on generating a URL based on CA.app.event_code equaling "test123";
During development in the browser, everything works fine, CA.app returns the variables I need.
When I compile my application with sencha app build and try to run the minified version in the browser, I get an error like this:
Error evaluating http://localhost:8888/app.js with message: TypeError: Cannot read property 'event_code' of undefined localhost:11
I'm not entirely sure why this is happening or how I can fix it. I am open to any and all ideas or suggestions, any pointers in the right direction will be greatly appreciated.
Ran into the exact same issue. You have no access to the namespaced app within the views... really sucks that they let you in development and not when built. Anyway, I got around it by adding a static helper class and using that all over my app:
In /app/util/Helper.js:
Ext.define('MyApp.util.Helper', {
singleton: true,
alternateClassName: 'Helper',
config: {
foo: "bar",
bat: "baz"
},
staticFunction: function() {
// whatever you need to do...
}
});
Then in your view or controller:
Ext.define('MyApp.view.SomeView', {
...
requires: ['Events.util.Helper'],
...
someViewFunction: function() {
var someValue = Helper.staticFunction();
// and you can use Helper.foo or Helper.bat in here
}
});
For reference, here's some documentation on Sencha Singletons. And one important note: make sure that your Helper singleton is in it's own file! If it's small, you may be inclined to put it at the bottom of your app.js, and things will work at first, and the build process will work, but the code will not. Don't worry, the build process puts all of your JS code in one big, compressed file anyway.

Resources