Running pytest test cases from test script

All the pytest documentation there indicates that py.test file_name is the standard way to run pytest test cases. But I am developing in Emacs with a Python script in the left pane and a Python interpreter in the right pane.

My workflow is that I make changes to the Python script, CTRL-C C it (so that the updated changes are sent to REPL) and move to REPL ( CTRL-O ) to test the updated code. It would be nice if I could make changes to my test case on the left, go to the right REPL screen and execute an updated test case.

Is there a way to run pytest test cases from REPL or inside a script, unlike spawning a shell and executing a pytest command?

EDIT . As an aside, I tried to use the main function from pytest and called it in a script, but for some funny reason, the changes were not raised unless I killed the old REPL and started a new one. This does not happen with another Python script, so I'm sure this is related to pytest. Here is the code:

 def test_add(): assert myadd(1, 2) == 3 if __name__ == '__main__': pytest.main() 
+4
source share
1 answer

Following remark @ Conrad.Dean. Testing in some kind of pytest.py seems to be a kind of useful convention. Below is an example illustrating the role of if __name__ == '__main__' :

 def myadd(first, second): return first + second def test_add(): assert myadd(1, 2) == 3 if __name__ == '__main__': print("Running test_add") test_add() 
0
source

Source: https://habr.com/ru/post/1491269/


All Articles