Python subprocess strange stdout indentation - python

I've been working on a git hook program, and in it, I'm using subprocess to run pytest, and it's giving some strange indentation in the stdout. This is running on git bash on windows, so it's certainly possible that it's a newline problem, but note that adding universal_newlines=True kwarg didn't help either
My calling code:
process_result = subprocess.run(['pytest', './test'], text=True)
My output:
$ git commit -m "t"
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.1.2, pluggy-1.0.0
rootdir: /mnt/c/Users/[usr]/Documents/barb
collected 1 item
test/test_cli.py . [100%]
============================== 1 passed in 0.56s ===============================
Any ideas on how to fix this issue?

Related

Can’t use allure's 'open' command to auto-open the report

I can't use allure's 'open' command to auto-open the report
The output shows that everything is OK
F:\python_venv\py310_selenium\Scripts\python.exe F:\PyCharm_project\temp_test_allure\main.py
============================= test session starts =============================
platform win32 -- Python 3.10.4, pytest-7.2.1, pluggy-1.0.0
rootdir: F:\PyCharm_project\temp_test_allure, configfile: pytest.ini
plugins: allure-pytest-2.12.0, rerunfailures-11.0
collected 1 item
test_aa.py . [100%]
============================== 1 passed in 0.07s ==============================
Report successfully generated to report_dir
Starting web server...
2023-02-02 21:16:04.127:INFO::main: Logging initialized #549ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://198.18.0.1:26299/>. Press <Ctrl+C> to exit
After these output, the browser will open but returns code 502:
Here is my code
# #File : main.py.py
import pytest
import os
if __name__ == '__main__':
pytest.main() #start pytest framework
os.system('allure generate temps_dir -o report_dir --clean') # generate report
os.system('allure open report_dir -p 0') # auto open report
Here is the pytest.ini
[pytest]
addopts =
--alluredir=./temps_dir
--clean-alluredir
I have configured the environment and installed the latest packages. When manually opening index.html in pycharm, it can be displayed normally. I don't know if it's allure's problem, or another plug-in problem:
How to allure to open the report normally?

pytest doesn't find new tests when run from python console within IDE

I have a file test_pytest.py
def test_dummy():
assert 1 + 1 == 2
I can run pytest from within a python console in an IDE such as Spyder (my use case is Blender but the behavior is general for an embedded python console)
pytest.main(['-v','test_pytest.py'])
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1 -- /home/elfnor/anaconda3/bin/python
cachedir: Documents/python/.cache
rootdir: /home/elfnor/Documents/python, inifile:
collecting ... collected 1 items
Documents/python/test_pytest.py::test_dummy PASSED
=========================== 1 passed in 0.01 seconds ===========================
I then add another test to test_pytest.py, and save the file.
def test_dummy():
assert 1 + 1 == 2
def test_another():
assert 2 + 2 == 4
And rerun pytest.main(['-v','test_pytest.py']) but pytest dosen't find the new test.
I've tried reloading the pytest module, but no luck. If I kill the console (in Spyder), re-import pytest and rerun, the new test is found.
I haven't found a way to kill the console in Blender (without killing Blender). Is there a different way to force pytest to find new tests when used in a console.?

Error in py.test in pycharm

When i run pytest on my system, it gives an error: no tests ran in 0.01 seconds . Can someone tell me if its because of error in my code or some other reason.
Output is :
============================= test session starts ==============================
platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/Desktop, inifile:
collected 0 items
========================= no tests ran in 0.01 seconds =========================
Process finished with exit code 0
I also had the same issue, the fix is to change the rootdir.
Go to Setting->tools->externaltools and set the parameters as "$FileName$" and working directory as "$FileDir$" for your pyTest

py.test on Ubuntu

I try to run a series of test cases on Ubuntu with py.test and is not collecting my test cases from a folder. I use unittest to write test cases.
On Windows i use this command:
py.test –v "folder with test cases" > log_file.txt
The output from Windows:
============================= test session starts =============================
platform win32 -- Python 2.6.3 -- py-1.4.20 -- pytest-2.5.2 -- C:\Python26\python.exe
plugins: capturelog
collecting ... collected 27 items
Same command on Ubuntu, the output is:
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-1.3.4
test path 1: TestScenario01/
=============================== in 0.01 seconds ===============================
I use different Python versions, because on Windows Scapy works only with Python 2.6. Another difference is that on Windows appears py-1.4.20 and pytest-2.5.2. I have them installed on Ubuntu too.
I managed to start tests. I had to remove the old py-1.3.4 module and install py-1.4.2 and also had to upgrade pytest to version 2.5.2. Now it works.

Should pytest.main('-s') run tests twice?

I've only just started learning about testing, and so I'm just starting out by trying to put together and run some very simple unit tests using py.test.
Example test_script.py:
import pytest
def test_func():
assert True
pytest.main('-v')
Running this gives:
============================= test session starts ==============================
platform win32 -- Python 3.3.1 -- pytest-2.3.4 -- C:\Program Files (x86)\Python33\python.exe
collecting ... collected 1 items
test_script.py:3: test_func PASSED
=========================== 1 passed in 0.12 seconds ===========================
If I replace -v with -s to view stdout (and disable pytest capturing of stdout), the tests run twice:
============================= test session starts ==============================
platform win32 -- Python 3.3.1 -- pytest-2.3.4
============================= test session starts ==============================
platform win32 -- Python 3.3.1 -- pytest-2.3.4
collected 1 items
test_script.py .
=========================== 1 passed in 0.04 seconds ===========================
collected 1 items
test_script.py .
=========================== 1 passed in 0.12 seconds ===========================
Should the tests run twice here? I did search, but couldn't find anything obvious in the documentation (though may have been looking in the wrong place).
That's a funny one :)
here is what happens: python executes test_script.py and thus executes pytest.main("-s") which goes back to the file system and collects the test_script.py as a test module. When pytest imports test_script, pytest.main(...) is invoked again during collection. The second invocation does not import test_script again because that is in sys.modules now, but it executes the test function. When the collection has finished (and the inner pytest.main run has executed the test once), the test function is also executed by the outer pytest.main invocation. Everything clear? :)
If you want to avoid this, you need to wrap the pytest.main invocation like this:
if __name__ == "__main__":
pytest.main("-s")
This invocation will not execute on a normal import but it will execute when you issue python test_script.py because python executes a command line specified script by setting __name__ to __main__ but to test_script upon a normal import test_script import.

Categories