hap497
hap497

Reputation: 162915

How to setup pytest in my python virtual environment

I follow the steps in https://sourabhbajaj.com/mac-setup/Python/virtualenv.html to setup a python virtual env in my directory by doing python -m venv testEnv And I did source venv/bin/activate. I am on MacOSX and zsh.

But when I do pytest, i get error like

 $ pytest                      
zsh: /Library/Frameworks/Python.framework/Versions/3.8/bin/pytest: bad interpreter: /usr/local/bin/python3: no such file or directory

And these are my python and pytest

❯ which python                  testEnv
python: aliased to /Users/hap497/.pyenv/shims/python
❯ which pytest                  testEnv
/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest

So what do i need to do to setup python and pytest in my Virtual env?

Upvotes: 2

Views: 3100

Answers (2)

Jortega
Jortega

Reputation: 3790

After your virtual environment is activated run pip install pytest

Here is an example of running my conftest.py function that prints "A" before my test function that prints "B".

cd to the parent directory, for this example it is py_tests and run.

pytest tests\tests.py -s -v

The output is:

A
setting up
B
PASSED

With directory structure:

py_tests
 -conftest.py
 -tests
  --tests.py

Files:

conftest.py

import pytest

@pytest.fixture(scope="function")
def print_one():
    print("\n")
    print("A")

test.py

import pytest

class Testonething:

    @pytest.fixture(scope="function", autouse=True)
    def setup(self, print_one):
        print("setting up")

    def test_one_thing(self):
        print("B")
        assert True

Upvotes: 1

wim
wim

Reputation: 362458

If you did

python -m venv testEnv

Then you wanted

source testEnv/bin/activate

And not

source venv/bin/activate

We can see in the which output that pytest is not correctly installed to your venv. Activate the testEnv correctly and install pytest again.

Upvotes: 1

Related Questions