Pytest passes when run in debug mode but fails otherwise - python-3.x

Following is the pytest code that I execute to test the add connection command.
def test_cli_connection_add(self, cmd, expected_output, expected_conn):
runner = CliRunner()
result = runner.invoke(connections, cmd)
assert result.output.strip() == expected_output
It fails with issue that tells the expected output should have newline.
> assert result.output.strip() == expected_output
E AssertionError: assert ('Successfully added `conn_id`=new4 : '\n 'hive_metastore://airflow:******#host:9083/airflow') == ('Successfully added `conn_id`=new4 : \n'\n 'hive_metastore://airflow:******#host:9083/airflow')
E + Successfully added `conn_id`=new4 : hive_metastore://airflow:******#host:9083/airflow
E - Successfully added `conn_id`=new4 :
E - hive_metastore://airflow:******#host:9083/airflow
tests/cli/commands/test_connection.py:558: AssertionError
To debug this issue further I added set_trace in the code:
def test_cli_connection_add(self, cmd, expected_output, expected_conn):
pytest.set_trace()
runner = CliRunner()
result = runner.invoke(connections, cmd)
assert result.output.strip() == expected_output
When I execute the code via debugger, the assertion pass. Can anyone help me why this works this way?

Related

md5(str.encode(var1)).hexdigest() giving hex value as 382fbe213f159eecf85facb256f265d0 - how to know the var1?

While running the test case, my code is failing as the expected value in hex is different then my answer.
for example, my ws_std value is 13.06 i.e. var1
md5(str.encode(var1)).hexdigest() giving hex value as 382fbe213f159eecf85facb256f265d0
But I am not sure if it matches with the hex value.
Getting error in below code :-
variables = ["ws_std", "p_range", "corr", "dew_month", "max_gust_month", "max_gust_value", "avg_temp", "temp_range", "max_p_range_day", "num_days_std", "median_b_days"]
answers = [ws_std, p_range, corr, dew_month, max_gust_month, max_gust_value, avg_temp, temp_range, max_p_range_day, num_days_std, median_b_days]
answer_dict = dict()
for var, ans in zip(variables, answers):
answer_dict[var] = md5(str.encode(ans)).hexdigest()
with open('test_files/hash.pk', 'rb') as file:
hash_dict = pickle.load(file)
def test_ws_std():
assert hash_dict["ws_std"] == answer_dict["ws_std"]
Error Code:-
========================================================== FAILURES ==========================================================
________________________________________________________ test_ws_std _________________________________________________________
def test_ws_std():
> assert hash_dict["ws_std"] == answer_dict["ws_std"]
E AssertionError: assert 'c8cc550afa85...2c6946c238f36' == '382fbe213f159...facb256f265d0'
E - c8cc550afa85496c4ee2c6946c238f36
E + 382fbe213f159eecf85facb256f265d0
test.py:40: AssertionError
see comments below .
output required : ws_std

Error in Groovy script: The current scope already contains a variable

I'm trying to write a groovy method for Jenkins pipeline. The method gets two variables, put them inside a string, and execute the string as a shell command.
Here is the method:
def method(A, B) {
test = "cp app/scripts/" + A + "config." + B + "js app/scripts/config.js"
def rc = sh(script: test, returnStatus: true)
if (rc != 0) {
error "Failed, exiting now..."
}
}
Here is the error i get when i execute this code:
The current scope already contains a variable of the name rc
# line 148, column 13.
def rc = sh(script: """
^
Does anyone know what could be the issue?

How to call only 1 Test Step from TestSuite using for loop or how to add assertion only in 2 test suite out of 3 test suit

I have to add assertion in 2 test suit out of 3, written below groovy script for adding assertion in all step, but it is considering "Groovy Script" Test Step also, and hence script is getting failed
def testCases = context.testCase.testSuite.getTestCaseList()
testCases.each
{
log.info "~~~Test Case:" + it.name
for(testSteps in it.testStepList)
{
log.info "~~~Test Step:" + testSteps.name
def Assertion5 = testSteps.getAssertionList()
for( a in Assertion5)
{
testSteps.removeAssertion(a)
}
def Assertion6 = testSteps.addAssertion("Contains")
Assertion6.setToken("REST Countries")
}
}
Your help will be appreciated. Thanks in advance.
Of course, groovy scripts steps are considered as testSteps, but testStep contains information about its type that you can filter on.
In your 'for' loop you should add a filter like :
if (testStep.config.type != "groovy")
and then do your processing.
That should work

Python3 cv2: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor only in a script file

this error error: (-215) scn == 3 || scn == 4 in function cv::cvtColor is only in my script, but if I ran the code in an IPython shell everthing works fine. The code is used to reduce dimensions for a DQN (Deeq-Q-network) running with openai-gym and I have no idea why this is not in my script but in my shell. I add a comment were the error appers. Full code can be added if needed.
Part of the code:
def preprocess(self,inp):
inp=cv2.resize(inp,(84, 110))
inp=cv2.cvtColor(inp, cv2.COLOR_BGR2GRAY)
inp=inp[26:110,:]
ret,obs=cv2.threshold(inp,1,255,cv2.THRESH_BINARY)
return np.reshape(obs,(84,84,1))
def Q_Learning(self,modell_path="Your path here"):
observation=self.env.reset()
for i in range(self.itertime):
observation=self.preprocess(observation)#!Error appers here!
if np.random.rand()<=self.random_move_prop:
action=np.random.randint(low=0,high=self.env.action_space.n)
else:
action=self.model.predict(observation)
action=np.argmax(action)
new_observation, reward, done, info=self.env.step(action)
new_observation=self.preprocess(new_observation)
self.storage.append([observation,action,reward,new_observation,done])
observation=new_observation
if done:
self.env.reset()

Retrieving value using xpath in SoapUI Groovy step

I need that the output of an operation GET ISM is transferred to the input of another operation SET ESM.
I want to recuperate talon=603090100042390 in this tag (this is response of GET ISM):
<privateUserId>603090100042390#xxxxxxxxxxxxxx</privateUserId>
I use this script:
groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
holder = groovyUtils.getXmlHolder("GET ISM#Response")
privateUserId = holder.getNodeValue( "//privateUserId" )
assert privateUserId != null
assert privateUserId.length() > 0
latlonNode = groovyUtils.getXmlHolder(privateUserId)
latlon = latlonNode.getNodeValue("//privateUserId")
log.info(latlon)
assert latlon != null
context["latlon"] = latlon
talon is input for SET ESM.
I have this Error :
Assertion failed: assert privateUserId != null | | null false Assertion failed: assert privateUserId != null | | null false error at line: 4
but i don't know why the problem in line 4.
I want to fixed this problem. Thank's
From your question it is not clear if there any namespaces in the response. Possibly that could be one of the reason to get null.
You could use Script Assertion for the first step.
assert context.response, 'Response is empty or null'
def pId = new XmlSlurper().parseText(context.response).'**'.find{it.name() == 'privateUserId'}.text()
log.info "privateUserId value : $pId"
assert pId, "Value of privateUserId is empty or null"
def userId = pId?.substring(0, pId?.indexOf('#'))
context.testCase.setPropertyValue('USERID', userId)
In the next step, wherever extracted user id is needed, use ${#TestCase#USERID}

Resources