3

I am able to run my pytest tests in my server /usr/local/bin/pytest --junitxml /proj/Results/result.xml /proj/unittests/.

But when run with Jenkins build option "Execute Shell". I am getting below error:

Traceback (most recent call last):

File "/usr/local/bin/pytest", line 6, in <module>     
import pytest

ImportError: No module named pytest Build step 'Execute shell' 
marked build as failure Finished
javabrett
  • 7,020
  • 4
  • 51
  • 73
VST
  • 123
  • 2
  • 3
  • 10

4 Answers4

3

I did something similar to what @afxentios suggested. Since I had no root privileges on the jenkins slave machine, I created virtualenv like this:

PYENV_HOME=$WORKSPACE/.pyenv/
virtualenv --no-site-packages $PYENV_HOME
source $PYENV_HOME/bin/activate
pip install -U pytest
pip install -r requirements.txt
py.test test_abc.py 
deactivate
Aliza
  • 734
  • 1
  • 10
  • 25
2

You need to install the module first:

pip install -U pytest

EDIT: You can try and create a virtualenv and execute your script inside in isolation.

#!/bin/bash

export WORKSPACE=`pwd`

# Create/Activate virtualenv
virtualenv testenv -p /path/to/python/bin
source testenv/bin/activate

pip install -U pytest 

# try to run your script here
....
afxentios
  • 2,502
  • 2
  • 21
  • 24
  • 1
    Is it really required. pytest working in command line. only from jenkins it is not working. – VST Dec 22 '16 at 11:36
  • That module already installed and no issue of running pytest from command line . pytest working in command line. From jenkins it is not working. even i ran pip install -U pytest. after that also i see same error. – VST Dec 22 '16 at 11:40
  • In that case I would have suggested you to create a `virtualenv `and run your script from there and check if that solve your issue. I have edited my answer. – afxentios Dec 22 '16 at 11:56
  • [workspace] $ /bin/bash /tmp/hudson3694367207590422445.sh Traceback (most recent call last): File "/usr/local/bin/virtualenv", line 7, in from virtualenv import main ImportError: No module named virtualenv – VST Dec 23 '16 at 07:20
  • It looks like that both ImportError issues is because of mixed version or python and/or pip. Do you have multiple version of them in your machine? If yes, you may find helpful the following discussions: http://stackoverflow.com/questions/10919569/how-to-install-a-module-use-pip-for-specific-version-of and http://stackoverflow.com/questions/21880909/setting-up-a-specific-python-in-jenkins . – afxentios Dec 23 '16 at 07:32
  • @ afxentios Yes, two version of python avilable (2.6 and 2.7).Discussion is talking about not picking right python version. For my case jenkins picking right version (2.7). *** Command line Pytest *** #python -m pytest === test session starts === platform linux2 -- Python 2.7.10, pytest-3.0.5, py-1.4.32, pluggy-0.4.0 *** pytest with jenkins in Execute Shell *** python -V python -m pytest Console Output -------------- python -V Python 2.7.10 + /usr/local/bin/python -m pytest /usr/local/bin/python: No module named pytest Build step 'Execute shell' marked build as failure – VST Dec 27 '16 at 00:49
0

It depends on how you have launched Jenkins. If you are using msi file for windows instead of jenkins war file then you are likely to get this error due to user profile setup.

In order to resolve this you need to open all services and right click on properties of your Jenkins service , go to Log on Tab and select log on with this accound. Enter your domainname/username for server into browser text box and click on browse. you will be able to see your full user name , select it ,enter your system password. click on ok.

After doing this restart your jenkins service. You have resolved issue!

0

I think you need to activate virtual env first, If you already have virtual env then you can do this:

CALL venv/Scripts/activate
pytest

It worked for me.