I have a rather simple question regarding output step messages in Codeception for which I found no answer in the documentation.
To be more precise: Is there anyway to "alter" the steps output in codeception using an alias?
This:
public function tryToTest(ApiTester $I)
{
$I->sendPOST('/', ['foo' => 'bar']);
$I->seeResponseCodeIs(200);
}
Will output:
I send post "/",{"foo":"bar"}
I see response code is 200
But I would like to alter the "content" with an alias so it can output:
I send post "/", NewCustomerRequest
I see response code is 200
Does Codeception have this capabilities?
No, Codeception has no such capability.
However the right place for such description is in the name of the test.
public function NewCustomerRequest(ApiTester $I)
Also you can use comment actions to add description to the output - amGoingTo, expect, expectTo and comment
https://codeception.com/docs/03-AcceptanceTests#Comments
Related
My scenario in the top feature file looks like this:
Scenario: Fetch a cat type id and create a cat
* driver.intercept({ patterns: [{ urlPattern: '*api/graphql*'}], mock:
'Mock.feature' })
When I click on the button to add a cat (//first graphql call happens. This gets cat types from the mock as expected)
And input a name
And select the cat type
And click create (//second graphql call happens here. But this returns the cat types again)
Then I see a success message
Here is the mock.feature:
Scenario: karate.match("request contains operationName: 'getCatTypes'")
def response = call read ('response.json')
Scenario: karate.match("request contains operationName: 'AddCat'")
def response = call read ('response1.json')
We use karate standalone jar and on version v1.2.0.RC1 (tried with 1.1.0 as well).
Appreciate any suggestions/directions.
Your use of karate.match() is wrong, note that it does not directly return a boolean. Read this for more: https://stackoverflow.com/a/50350442/143475
Try this change:
Scenario: karate.match("request contains operationName: 'getCatTypes'").pass
I also think there are much better ways to do the above, so read this also: https://stackoverflow.com/a/63708918/143475
I have below java class which runs with cucumberOptions
#CucumberOptions(tags = {"#userManagement"})
public class IC_API_Tests_Runner {
runner code here
}
From jenkins I am passing below command ti run the tests
clean test "-Dkarate.env=$WhereToRun" "-Dbvt.tags=#userManagement"
I am able to fetch the value of 'bvt.tags' using below command
bvtTags = karate.properties['bvt.tags'];
Now I need to pass the 'bvtTags' value to the CucumberOptions.
I tried
#CucumberOptions(tags = {"bvtTags"})
public class IC_API_Tests_Runner {
runner code here
}
But 'bvtTags' value is not substituted in the CucumberOptions. But I am able to print the value of 'bvtTags' with print statement in karate code.
Any help will be great help
No you can't do dynamic changing of the #CucumberOptions like that.
Use the API for dynamically choosing tests, see this example: DemoTestSelected.java.
Then do something like this (please change for your environment):
String tags = System.getProperty("bvt.tags");
List<String> tags = Arrays.asList(tags);
EDIT: actually you don't need to do any of this. (I guess that you will never read the docs :)
Please refer: https://github.com/intuit/karate#command-line
-Dkarate.options="--tags #userManagement"
I'm using the testing framework Codeception to do BDD. I understand the idea of wanting something, but I don't understand what the function does.
$I->wantTo('Understand what this method does!');
http://codeception.com/docs/03-AcceptanceTests#Comments
Commands like amGoingTo, expect, expectTo help you in making tests
more descriptive.
$I->wantTo('Understand what this method does!');
will be rendered as * I want to understand what this method does! in verbose output.
Update 2022-11-16:
My original answer was incorrect, wantTo is not a comment method, it renames test method in the output.
Example:
I created very simple Cest class:
<?php
class ExampleCest
{
public function provideExample(CliGuy $I)
{
}
}
When I ran it, I got the following output:
Cli Tests (1) --------------------------------------------
U ExampleCest: Provide example (0.00s)
---------------------------------------------------------
but after adding $I->wantTo('change test name!'); to method:
I got the following output:
Cli Tests (1) --------------------------------------------
U ExampleCest: Change test name! (0.00s)
---------------------------------------------------------
The benefit of wantTo is that it allows to use characters not permitted in method names or different formatting than automatically generated.
I looked up if wantTo has any documentation and all I found was old blog post using examples in class-less Cept format (which is deprecated and is likely to be removed in Codeception 6).
<?php
$I = new TestGuy($scenario);
$I->wantTo('log in to site');
$I->amOnPage('/');
$I->click('Login');
$I->fillField('username', 'admin');
In Cept format wantTo had better purpose, because it didn't override anything, but provided additional information next to file name.
I am working with:
Spock Core
Spock Reports
Spock Spring
Spring MVC Testing
and I have the following code:
def "findAll() Expected"(){
given: "The URL being used is: /personas/xml/personas"
url = PersonaUrlHelper.FINDALL;
when: "When the URL is being calling with a GET"
resultActions = mockMvc.perform(get(url)).andDo(print())
then: "something..."
resultActions.andExpect(status().isOk())
.andExpect(content().contentType(RequestMappingProducesJsonUtf8.PRODUCES_JSON_UTF_8))
}
Two observations:
One: observe given: "The URL being used is: /personas/xml/personas" where the URL/URI value has been added manually.
Two: the url variable has been defined how an instance variable, because it is common in many test methods. Therefore def String url
My question is:
how I can display the url variable in a Spock's label/block? how (given, then…)? It to be printed through Spock Reports and improve my testing documentation
I have read the following:
Spocklight: Extra Data Variables for Unroll Description
It working around #Unroll. But I did realize all work around the where label/block.
I already have tried something like:
given: "The URL being used is: $url"
given: "The URL being used is: ${url}"
And does not work
I want to work around with a syntax similar like the following:
def "findAll() Expected"(){
url = PersonaUrlHelper.FINDALL;
given: "The URL being used is: $url"
…. something
when: "When the URL is being calling with a GET"
So what could be the correct configuration?
Asume I do a refactor for PersonaUrlHelper.FINDALL used in some Spring's #RequestMapping and in this test method. I don't want update manually the text in the given label/block
So what is the correct syntax?
Quick answer:
I guess the where-block approach will be the right one. Use something like
where: "When the URL is being calling with a GET"
url << PersonaUrlHelper.FINDALL
And remove the definition of url from the test. You will be able to use the url variable, since it is definied in the where-block. And you will be able to reference it from the test description as #url:
#Unroll
def "findAll() Expected"(){
given: "The URL being used is: #url"
//removed url definition
when: "When the URL is being calling with a GET"
resultActions = mockMvc.perform(get(url)).andDo(print())
then: "something..."
resultActions.andExpect(status().isOk())
.andExpect(content().contentType(RequestMappingProducesJsonUtf8.PRODUCES_JSON_UTF_8))
where: "When the URL is being calling with a GET"
url << [PersonaUrlHelper.FINDALL]
}
Another more hacky way would be to print the url just through a println url - this output is also captured afaik, but it wouldn't be as nice.
Update: please take a look at the following spock console script: https://meetspock.appspot.com/script/5146767490285568 :
import spock.lang.*
class PersonalUrlHelper {
final static String FINDALL = 'http://example.com'
}
class MyFirstSpec extends Specification {
#Unroll
def "findAll() Expected #url "(){
given:"The URL being used is: #url"
when: "When URL (#url) is assigned to a"
def a = url
then: "a equals the URL (#url)"
a == url
where: "the URL is fetched from another class or map in this case"
url << [PersonalUrlHelper.FINDALL]
}
}
I tried to mock your script - without having your code.
As you can see, the content of the URL is printed out in the test name. AFAIK, it will also be reflected in the texts of the different test blocks when printed out through spock-reports.
BTW: the [] are important since they turn the returned string into a list with one element. Otherwise the string will be interpreted as lsit and the test will iterate through each character.
Does this help?
In SoapUI, I have a host Test Case, which executes another external Test Case (with several test steps) using the "Run Test Case" test step. I need to access a response from the external TC from within my host TC, since I need to assert on some values.
I cannot transfer the properties since they are in XML. Could I get some pointers as to how I could leverage Groovy/SoapUI for this.
For Response you can use the below code.
testRunner.testCase.getTestStepByName("test step").testRequest.response.responseContent
In you external TC create another property and at the end of the TC use Transfer Property step to transfer your XML node to it. In your host TC, just read that property as you would any other.
I also had a look around to see if this can be done from Groovy. SoapUI documentation says that you need to refer to the external name of the testsuite / testcase:
def tc = testRunner.testCase.testSuite.project.testSuites["external TestSuite"].testCases["external TestCase"]
def ts = tc.testSteps["test step"]
But I could not find how to get at the Response after that.
In addition to Guest and SiKing answers, I share a solution to a problem that I've met:
If your step is not of type 'request' but 'calltestcase' you cannot use Guest answer.
I have a lot of requests contained each in a testCase and my other testCases call these testCases each time I need to launch a request.
I configured my request testCases in order to return the response as a custom property that I call "testResponse" so I can easily access it from my other testCases.
I met a problem in the following configuration :
I have a "calltestcase" step that gives me a request result.
Further in the test I have a groovy script that needs to call this step and get the response value
If I use this solution :
testRunner.runTestStepByName("test step")
followed by testRunner.testCase.getTestStepByName("test step").testRequest.response.responseContent
I'm stuck as there is no testRequest property for the class.
The solution that works is :
testRunner.runTestStepByName("test step")
def response_value = context.expand( '${test step#testResponse#$[\'value\']}' )
another solution is :
testRunner.runTestStepByName("test step")
tStep = testRunner.testCase.getTestStepByName("test step")
response = tStep.getPropertyValue("testResponse")
Then I extract the relevant value from 'response' (in my case, it is a json that I have to parse).
Of course it works only because I the request response as a custom property of my request test case.
I hope I was clear enough