How do I pass the same parameter to different tests in Elixir?
Now I calculate it in every test. Is it possible to calculate this parameter once before starting tests and pass it to each test?
Use ExUnit.Callbacks.setup/1 or ExUnit.Callbacks.setup_all/1
setup_all do
[my_param: 42]
end
test "global context", %{my_param: value} do
assert 42 == value
end
Related
Find the example here.
def a = condition ? " karate match statement " : "karate match statement"
Is it possible to do something like this??
This is not recommended practice for tests because tests should be deterministic.
The right thing to do is:
craft your request so that the response is 100% predictable. do not worry about code-duplication, this is sometimes necessary for tests
ignore the dynamic data if it is not relevant to the Scenario
use conditional logic to set "expected value" variables instead of complicating your match logic
use self-validation expressions or schema-validation expressions for specific parts of the JSON
use the if keyword and call a second feature file - or you can even set the name of the file to call dynamically via a variable
in some cases karate.abort() can be used to conditionally skip / exit early
That said, if you really insist on doing this in the same flow, Karate allows you to do a match via JS in 0.9.6.RC4 onwards.
See this thread for details: https://github.com/intuit/karate/issues/1202#issuecomment-653632397
The result of karate.match() will return a JSON in the form { pass: '#boolean', message: '#string' }
If none of the above options work - that means you are doing something really complicated, so write Java interop / code to handle this
What happens if you assign a a Unit type to a variable in Kotlin?
Lets use this small piece of code as an example:
val list = listOf("Hey", "this", "code", "runs")
val unit = list.forEach {
print(it.plus(" "))
}
Output:
Hey this code runs
Why does this code run? What is the value of val unit in this example? I mean, shoudn't there be a unit.invoke() function to actually run the code?
Thanks in advance!
In Kotlin, Unit is a type that has exactly one value: the object also called Unit.
It has the same purpose as the void type in Java, but the implementation is different. (This is because Kotlin borrows ideas from functional languages, in which every expression has a value. So if a function has nothing useful to return, it returns Unit instead. This distinguishes it from methods that never return, declared with the Nothing type which has no values.)
Your code is perfectly valid; the forEach() executes and prints its output, and then returns the Unit value afterward. (forEach() is one of those functions you call only for its side-effects, not for its return value. That's why you don't usually bother doing anything with the return value; but you've shown that you can if you really want to!)
So your unit is inferred to have the type Unit, and will be assigned the Unit value.
You could check this by printing it afterward:
println(unit)
That prints "kotlin.Unit", which is the result of calling toString() on the Unit value.
Why does this code run?
It makes semantically and syntactically sence for the language Kotlin. Both initialisations of your variables are return values of the functions in front of them.
-
What is the value of val unit in this example?
unit is Unit - like void in Java - because foreach returns Unit
val unit : Unit = list.forEach {
print(it.plus(" "))
}
-
shoudn't there be a unit.invoke() function to actually run the code?
The varible unit is initialised with the execution of forEach. To get the result it invokes the loop, which executes print(it.plus(" ")) .
EDIT
What happens if you assign a a Unit type to a variable in Kotlin?
Nothing. There is no need to save/store it.
I have an Config Element in JMeter, Especially User Defined Variables.
I have the variable "user" with the value "Justin", How can I use this variable in the groovy code (of an JSR223 Assertion)?
There are several of getting it:
Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as Parameters which contains the full string passed in the "Parameters" section
Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as args[0] (if you pass more than one variable separated by spaces you will be able to refer 2nd variable as args[1], 3rd as args[2], etc.
You can access it as vars.get('user') where vars stands for JMeterVariables class instance
You can access it as vars['user'] - basically the same as point 3 but uses some Groovy Syntax Sugar
You can access it as ctx.getVariables().get('user') - where ctx stands for JMeterContextService class instance just in case (in some test elements vars shorthand is not available)
Demo:
Any JSR223 element including Assertion have few variables it can use out of the box.
One of the variable is vars which is basically a map of JMeter stored variables.
User Defined Variables row is creating a JMeter variable, so you can get your value Justin in JSR223 using vars.get("user")
I tried to create a variable in groovy and Jmeter.
I want it to be the counter of a while loop that I am planning to run.
I want in each iteration to add 1 to this counter.
the problem is that I can not set it to be variable, that I could use later in the program in another sampler.
int while_counter = 0;
vars.put("while_counter",0);
System.out.println("Loop Counter");
I just want to create an integer that will be a counter and all the sampler will know, and can address him ${while_counter}.
and to perform while_counter = while_counter++
what I am missing
Response code: 500
Response message: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer) values: [while_counter, 0]
Possible solutions: put(java.lang.String, java.lang.String), get(java.lang.String), putAt(java.lang.String, java.lang.Object), wait(), any(), dump()
Can someone please advise how to create a simple while loop in Jmeter
and to add 1 to counter
Define variable "i" as 0/1 at start of Test Plan in User Defined Variable/User Parameters
While loop condition:
${__groovy(vars["i"].toInteger() < 5)}
Inside loop add your JSR223 Sampler/Preprocessor with increment it:
String i = vars.get("i");
int counter = Integer.parseInt(i);
counter++;
vars.put("i", "" + counter);
Don't use Javascript function as it doesn't scale as well as Groovy
It is recommended to avoid scripting where possible so:
If you are running an "internal" loop driven by Loop Controller or While Controller it makes sense to add a Counter test element which will generate an incrementing number each time it will be called. You will be able to refer counter's current value using "normal" JMeter variable defined in the counter's "Reference Name" section
For Thread Group level iterations you can use __iterationNum() function
With regards to your question itself, you cannot pass an integer to vars.put() method if you need to store an integer in JMeter Variables you should go for vars.putObject() method instead.
How do I test that a given helper method only takes exactly one argument?
I thought of doing this:
describe "#textile" do
it "should take only one argument" do
textile().should raise_error
end
end
but that seems to still break the test, with the error wrong number of arguments 0 for 1.
Regardless of why you'd want to test this, here's one way to write it:
describe "#textile" do
it "should fail when given no arguments" do
expect { textile() }.to raise_error ArgumentError
end
it "should accept one argument" do
expect { textile("foo") }.not_to raise_error ArgumentError
end
end
Note that you could leave off the ArgumentError and just say that these invocations should or should not raise an error, but by specifically saying they should or should not raise ArgumentError, you're isolating the case that you're trying to specify. textile("foo") may raise some other kind of exception but will still pass the second example.
Actually, you can test arity directly.
>> method(:hello).arity
=> 2
You can get different answers based on those given defaults, plus any *args as well.
You will want to read the documentation that describes this:
Returns an indication of the number of arguments accepted by a method.
Returns a non-negative integer for methods that take a fixed number of
arguments. For Ruby methods that take a variable number of arguments,
returns -n-1, where n is the number of required arguments. For methods
written in C, returns -1 if the call takes a variable number of
arguments.
So, in rspec, you would write your test accordingly, testing arity, rather than testing if it raises errors or not.