Test name as given with collect-only - pytest

I would like to access the name of the current test as a string in my tests to write some VCD log-files.
Is the name as given when I run pytest --collect-only available as a fixture or equivalent?
Example:
Running pytest --collect-only yields (shorted):
<Class TestFooBar>
<Function test_30_foobar[1-A]>
In my test I would like to access the above string test_30_foobar[1-A].
Is there a (simple) way?

I've found an answer for my own question. It's hidden in the request fixture. See the following thereof derived fixture:
#pytest.fixture
def name_test(request):
"""Make name of test available as string and escaped as filename"""
import types
i = types.SimpleNamespace()
i.name = request.node.name
i.filename = i.name.replace('[', '_').replace(']', '')
return i
The filename variable is a clumsily escaped string that should be a valid filename. However, only tested on POSIX so far.

Related

pytest get command line options

I have couple of questions regarding commandline options of pytest
If it is possible to use long names and short names for the same option, for example
parser.addoption('--server', '-srv', dest = 'SERVER')
How to access to commandline option by name, like :
config.option.NAME
def environment_options(parser):
parser.addoption('--server', dest= "SERVER")
#pytest.fixture()
def enfironment_set_up():
if config.option.SERVER == 'some value':
actions
pycharm shows reference unresolved 'config'. Do I need to import something ?
As far as I know (haven't found that in the documentation), it is possible to add a short name, but only one with one upper case letter, e.g.:
def environment_options(parser):
parser.addoption('-S', '--server', dest= "SERVER")
Lowercase letters are reserved for pytest itself, and longer abbreviations are not supported. See also my somewhat related answer.
You can access the option via config in the request fixture:
#pytest.fixture
def enfironment_set_up(request):
if request.config.getoption("SERVER") == 'some value':
actions

py.test: How to avoid naming every test? Generate the test name dynamically

Occasionally I name tests like test_X1, test_X2,...
because
it is always about feature X and
the tests are small and
I don't want a descriptive name that is longer than the test
Especially when things still change a lot I don't want to think of a name at all.
The line where the test resides defines the test in the file.
So how to use the line for the test name?
Here is a way to name the tests dynamically test_<line>.
from inspect import currentframe
def namebyline(f):
line = currentframe().f_back.f_lineno
globals()['test_' + str(line)] = f
#namebyline
def _(): # becomes test_6
assert True
#namebyline
def _(): #becomes test_9
assert False

Adding user defined keywords or Test Attribute

I would like to add custom attributes (or Keywords) to a test which I can access during pytest_runtest_logreport.
What I have been doing currently is to set a marker like this
#pytest.mark.TESTID(98157) for tests and then use this in pytest_runtest_logreport as report.keywords['TESTID'] which returns a tuple of length 1 having value 98157. So far so good.
But when I tried to add another marker with defect ID like this #pytest.mark.JIRA("MyJIRA-124") this report.keywords['JIRA'] this gives me integer 1.
So my question is can we not create parameterized marker with string parameters
AND
If that is the could be the probable workaround for me.
Unfortunately "report" will not have this vaules in default implmentation as it's only a dict with 1 as values for each key (source code)
I think that the easiest workaround would be to change how the "report" is constructed using pytest_runtest_makereport hook. It could be as simple as this:
from _pytest.runner import pytest_runtest_makereport as _makereport
def pytest_runtest_makereport(item, call):
report = _makereport(item, call)
report.keywords = dict(item.keywords)
return report
Then in pytest_runtest_logreport, under report.keyword['JIRA'] you will find MarkInfo object

pytest - Override a test funciton defaullt value

i have a pytest testconf and a test function
i want that when running:
pytest -q -option="choose_a"
it would pass A
and when running
pytest
it would pass B
the problem is that it does not see 'my_param' in fixtures since it has a default parameter. what can I do to make this working and elegant?
my current temp solution (which I don't like) is removing the default value and assign it with an "if" statement in conftest.pytest_generate_tests....pls give me a better one :)
conftest.py:
def pytest_addoption(parser):
parser.addoption("--myoption", action="append", default=[], help="list of choices")
def pytest_generate_tests(metafunc):
if 'my_param' in metafunc.fixturenames:
if 'choose_a' in metafunc.config.option.myoption:
metafunc.parametrize("my_param", ["A"])
mytest.py:
def test_my(my_param='B'):
do_stuff(my_param)

By using groovy scripts local property value is not updated in SOAP UI

I have written below groovy script to update the local test case property values in Properties tab:
String testString = "TestString"
testRunner.testCase.setPropertyValue( "Pro_Response", testString )
def getLocalPropValue = testRunner.testCase.getPropertyValue("Pro_Response")
log.info(getLocalPropValue)
So after running this groovy script my expected out put is Pro_Response property should be updated with testString value. But this is not happening.
Note: There is no issues wit the groovy log.info(getLocalPropValue) is giving me the testString value in script output.
Can anyone plse suggest
I have found below solution for this issue:
Since I was looking to modify the property in Properties test step:
I need to modify below existing line of code:
testRunner.testCase.setPropertyValue( "Pro_Response", testString )
Like below:
testRunner.testCase.testSteps["Properties_2"].setPropertyValue( "Pro_Response", testString )