Currently testing file name in pytest [duplicate] - pytest

What I'm trying to do
I'm writing a small framework in python using pytest, and as part of the teardown I am taking a screenshot.
Now, I want that screenshot to be named according to the running test, not conftest.py
So, for example, my code right now is:
driver.save_screenshot(os.path.basename(__file__)+'.png')
The problem
This prints the name "conftest.py".
I want to print the calling test name if possible. I am guessing using requests?

You can access the current file path via request.node.fspath. Example:
# conftest.py
import pytest
#pytest.fixture
def currpath(request):
return str(request.node.fspath)

Related

Print available command line options using pytest

I need to print the command line options using pytest and I am following this thread: How can I see the current pytest configuration?
The code snippet mentioned works. However, this gives all the pytest options as well. I want to print just the command line options which are available in the code (added by using pytest_addoption). Is there a way to do this?
Thanks
# test_sample.py
import pytest
import os
def test_answer(pytestconfig):
for item_name in dir(pytestconfig.option):
if not item_name.startswith('_'):
print(f'{item_name}: {getattr(pytestconfig.option,item_name)}')
assert 0 # to see what was printed

Is there a way in pytest to generate test report from custom file for non-python test cases?

Background
Trigger legacy non-python testcases from pytest. Since these testcases are categorized as testsuites, from pytest perspective we'll be doing an ssh on a remote machine and trigger a testsuite. So from pytest's point of view it is a single testcase, but actually it would be a bunch executing on remote machine.
Requirement
The testsuite will generate a testreport which we'll SCP back to the pytest machine. I wish to parse the testreport and report the PASS/FAIL for each testcase from pytest
I have been looking into example but still can't get my head around on how would I trigger the test case with SSH and parse the testreport(XML/JSON) and generate pytest report
Any suggestions ?
Update:
I have been able to parse the yaml file to generate the terminal report(pytest_terminal_summary) for my testcases. But I would like that pytest also reports the number of testcases failed/passed.
Can you try pytest test.py -v --junitxml="result.xml"
You can also generate html result using pytest file.py -sv --html report.html

How to run a pytest-bdd test?

I am not understanding how to properly run a simple test(feature file and python file)
with the library pytest-bdd.
From the official documentation, I can't understand what command to issue to run a test.
I tried using pytest command, but I saw the NO test ran.
Do I need to use another library behave to run a feature file?
I figured out trying for 2 days,that ,
for running a pytest-bdd test, there are certain requirements, at least in my view.
put both the feature file and python file in the same directory (maybe this can be changed with configuration files)
the python file name needs to start with test_
the python file needs to contain a method of which name will start with test_
the method starting with test_ , need to be assigned to the #scenario sentence
to run the test, issue pytest command in the same directory(maybe it is also configurable)
After issuing you will only see the method with the name starting with test_ has passed, but all the tests actually ran. To test, you can assert False in any #when or #then annotated method, it will throw errors.
The system contained : pytest-bdd==3.0.2 (copied from pip freeze output)
Features files and python files can be placed in different folders using the bdd_features_base_dir hook provided by pytest-bdd; I think it is better having features files in different folders too.
Here you can see a working example (a simple hello world BDD test):
https://github.com/davidemoro/pytest-play-docker/tree/master/tests
https://github.com/davidemoro/pytest-play-docker/blob/master/tests/pytest.ini (see bdd_features_base_dir in [pytest] section)
https://github.com/davidemoro/pytest-play-docker/tree/master/tests/bdd
If you want to try out pytest-bdd without installation you can use Docker. Create a folder with inside your pytest BDD files and if you want a separate features folder targeted in bdd_features_base_dir and run:
docker run --rm -it -v $(pwd):/src davidemoro/pytest-play:latest
I've found out, that in the python file you don't have to put:
the method starting with test_ , need to be assigned to the #scenario sentence
You can just add: scenarios("") - to allow the tests to be started, which are using steps defined in this specific python file.
Remember to import scenarios!: from pytest_bdd import scenarios
Example:
Code example
Command..
pytest -v path_to_test_file.py
Things to note here..
Check format of feature file as filename.feature
Always __init__ modules, otherwise test-runner will not find test files
Glue right step definitions to test function
Add feature in features module
If you are using python3 execute test with python3
So,
python3 -m pytest -v path_to_test_file.py
Documentation
https://pytest-bdd.readthedocs.io/en/stable/#

Not able to generate the code coverage result using Devel::Cover

I tried to find code coverage for a c4rgr.pl test file with Devel::Cover. I have a .pm module sitting in the same area. I am using perl -MDevel::Cover c4rgr.pl, which generates a cover_db and has a runs subdirectory inside.
Inside the runs subdirectory, a versionated directory is created every time I run the Cover command and a cover.13 is obtained. This cover.13 file (I think) has the raw data for code coverage results.
Still, I am unable to get the test results in a HTML output format or on the Terminal screen.
This is what I do:
Run the perl -MDevel::Cover c4rgr.pl
Here, the c4rgr.pl uses a .pm module which is sitting in the same area.
When I run above command, it runs the test file but no test coverage output as shown in Devel::Cover HTML output is obtained. Nevertheless, the cover.13 file is created every time.
Cover.13 <- 1401378982.24872.12631 <- runs <- cover_db .
Can someone tell me how to get the HTML file, please? I was able to run a dummy test with the same features and generate a HTML file. But with my actual test, it does not. Or is there a way to convert the cover.13 file to HTML that is not being done in my case?
After running perl -MDevel::Cover c4rgr.pl, run the cover command. For example:
cover -report html -outputdir cover_report

Execute external command

I do not know whether it is a Scala or Play! question. I want to execute some external command from my Play application, get the output from the command and show a report to user based on the command output. Can anyone help?
For example, when I enter my-command from shell it shows output like below, which I want to capture and show in web:
Id Name IP
====================
1 A x.y.z.a
2 B p.q.r.s
Please, do not worry about format and parsing of the output. Functionally, I am looking something like PHP exec. I know about java Runtime.getRuntime().exec("command") but is there any Scala/Play version to serve the purpose?
The method !! of the Scala process package does what you need, it executes the statement and captures the text output. For example:
import scala.sys.process._
val cmd = "uname -a" // Your command
val output = cmd.!! // Captures the output
scala> import scala.sys.process._
scala> Process("cat temp.txt")!
This assumes there is a temp file in your home directory. ! is for actual execution of the command. See scala.sys.process for more info.
You can use the Process library: for instance
import scala.sys.process.Process
Process("ls").!!
to get the list of files in the folder as a string. The !! get the output of the command