0
votes

I have problem in importing pytest while writing a python code. "import pytest is grayed out.

Python is 3.8.3, Pycharm community edition. pytest version 5.4.2, is successfully installed and can be seen in the project interpreter in pycharm. As well as I can see the installed path of pytest in python directory.

When running py.test command from console. It starts the test run shows "collected 0 items" and lastly ends with "NO TESTS RAN IN 0.05s"

If anyone running similar problems with some other packages kindly let me know. TIA...

2
Can u post the code your trying to execute. - sushanth
that it is greyed out only indicates it is not being used in the file ... i suspect you are simply not aware of how to invoke pytest correctly (ie you don't usually import pytest into a file... but you can and sometimes need to) - Joran Beasley
Make sure you have named your tests (and test class if you use one) correctly. Also, you should use pytest instead of the deprecated py.test - MrBean Bremen
Thanks everyone for the prompt and clear suggestions. - ApI-QA
@JoranBeasley: Thanks I now understand what you are trying to say. You are correct I am not aware of it. This is first time I am using Python and Pytest so many features I do not know how to implement :) - ApI-QA

2 Answers

0
votes

You simply run pytest from the commandline. There is no need to import pytest into a script. Take this Python script as an example:

def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 4

To run pytest on it, from the terminal (after changing to the right directory):

$ pytest

And you will then see the test outcome in the commandline as pytest automatically picks up the python scripts names test_*.py, where * is any name, e.g. test_increment.py. To have a test from your Python script run, name it with test_ as well to begin with.

0
votes

Running pytest in the terminal is an option. In addition, Pycharm has integrated test suite for automatic discovery and collection of test tasks. You can use hotkey ctrl+shift+10 to run the test tasks directly in current file .

enter image description here