Home > OS >  sytanx error running pytest from terminal
sytanx error running pytest from terminal

Time:12-11

I'm trying to use pytest with a simple example, saved as "test_lesson1.py" with directory structure as shown below.

import pytest

TOL = 2e-2

def squared(x):
    return x**2

def test_squared():
    x = 4
    expected = 16
    computed = squared(x)
    msg = "fail"
    np.testing.assert_allclose(expected, computed, rtol=TOL, err_msg=msg)

Example directory structure:

proj
|--tests
|--|--test_lesson1.py

On Windows but using Git Bash as a bash terminal, how can I run pytest? Here is what I am entering the terminal:

alias py='C:/Users/name/anaconda3/envs/myenv/python.exe'
. C:/Users/name/anaconda3/etc/profile.d/conda.sh
conda activate myenv 
cd tests/
py -c "pytest test_lesson1.py"

which returns

 File "<string>", line 1
    pytest test_lesson1.py
           ^
SyntaxError: invalid syntax
(myenv)

I can confirm that python works with py -c "print('hello world')", which prints as expected.

CodePudding user response:

pytest is a command that will execute all tests in all files whose names follow the form test_*.py or \*_test.py in the current directory and its subdirectories, therefore you don't need python to invoke it.

Just run

pytest test_lesson1.py

On the other hand, print is a python function and you need python to invoke it.

  • Related