30

I try to use nosetests
❯ nosetests '/pathTo/test'

but it uses python 2.7 for my tests:

sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)

So some of them fails, because they were written in python 3.3.

I work it around and installed virtual environment:

pyvenv-3.3 py3env

Activated it:

source ~/py3env/bin/activate

Check python virsion in virtual environment:

❯ python --version                                                                                 ⏎
Python 3.3.3
(py3env)

Ok. But nosetest still uses python2.7 even in virtual environment:

sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)

So my tests fails. How to make nose use python3?

ArekBulski
  • 4,520
  • 4
  • 39
  • 61
Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166

4 Answers4

37

In Python 3.4 and higher versions: in order to make nose use python3 just run ...

python3 -m "nose"

... in the target directory with the tests.

The environment setups are not required.

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
8

To install:

sudo apt-get install python-nose python3-nose

To run:

nosetests-2.7 ; nosetests3

This runs the test suite under both PY2 and PY3.

ArekBulski
  • 4,520
  • 4
  • 39
  • 61
  • 1
    Can I only install the python3 version? and then use it like: `sudo apt install python3-nose` `nosetests3` – Rea Haas Jun 29 '20 at 16:02
  • 2
    After I tested this, it's possible to only install the `python3-nose` version, run as: `sudo apt install python3-nose` && `nosetests3` – Rea Haas Jun 30 '20 at 08:49
2

I found way to use nosetests with python3 without environment:

cd /Library/Frameworks/Python.framework/Versions/3.3/bin  

And then:

❯ nosetests-3.3 '/folder/with/tests'

nosetests-3.3 uses python 3

That's it.

And if your want use command nosetests instead of nosetests-3.3, add in ~/.bash_profile:

nosetests()
{
    /Library/Frameworks/Python.framework/Versions/3.3/bin/nosetests-3.3 $1
}

Now you can use:

nosetests '/folder/with/tests'

from any directory. It uses python3.

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
  • Alternatively, you can link nosetests-3.3: ```ln /Library/Frameworks/Python.framework/Versions/3.3/bin/nosetests-(version-number) /usr/local/bin/nosetests3``` – nicolai.tesela Sep 01 '17 at 17:19
  • Better to use `"$@"` instead of `$1`. That way you pass _all_ arguments through, quoting is preserved, globbing is suppressed when quoting indicates that it should be, etc. – Charles Duffy May 04 '21 at 03:36
2

This isn't a virtualenv issue as much as a linux issue.

This means that when you use the command nosetests from the terminal, linux looks within it's available paths (/bin, /sbin, or whatever it is by you) for such an executable file.

Your global python 2 nosetests is found first and executed.

Your virtualenv python3 nosetests is later in the available path list and therefore never reached.

I would suggest to only install nose or any other python command per virtual environment.

l__flex__l
  • 1,388
  • 1
  • 16
  • 22