0

I run my python unit tests by executing my test file through the shell and invoking

if __name__ == '__main__':
    unittest.main()

at the end of the test file.

I have to support python 2.7 and python 3 (I am writing a plugin for Sublime Text.)

My shell runs python 2.7, which came with my version of Mac OS X. Sublime Text 3 comes bundled with it's own version of Python (some flavor of python 3). Is there a way I can run my test files both with python 2.7 and with python 3 (hopefully without having to install yet another version of python) from the shell?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
DudeOnRock
  • 3,685
  • 3
  • 27
  • 58
  • Are you asking how to execute your code with both of the Python versions on your system, or how to make your code compatible with both? They're both reasonable questions, I'm just not sure which one you're asking. – Blckknght Nov 04 '13 at 20:54
  • 1
    @Blckknght: I want to execute the code with both versions! Thanks making that destinction. – DudeOnRock Nov 04 '13 at 21:01

2 Answers2

3

The standard tools for this problem are six, to help you write a script that will run equally well under python2 and python3, and tox, to help you run your test suite on several python versions. Once you have your script working and tested, and tox configured, you just run tox to run your test suite under your various pythons.

bukzor
  • 37,539
  • 11
  • 77
  • 111
  • Thanks! I'll give tox a test drive (pun intended.) My preliminary understanding is that tox uses virtual environments. Since there are two *actual* versions of python running on my system, is there a way to run the tests with those installed, without installing a third party program? – DudeOnRock Nov 04 '13 at 21:09
  • Installing the package (using your setup.py and virtualenv) helps ensure that users will get the same experience as you, the developer. Since this is a core feature of tox, I don't think they provide a mode to disable it. – bukzor Nov 04 '13 at 21:59
  • @DudeOnRock: any results? – bukzor Nov 07 '13 at 20:20
0

There are presentation slides about running code on both 2.x and 3.x, at http://stromberg.dnsalias.org/~dstromberg/Intro-to-Python/

I've never used Sublime, so I can't help you with that part.

dstromberg
  • 6,954
  • 1
  • 26
  • 27