4

I have used the 2to3 utility to convert code from the command line. What I would like to do is run it basically as a unittest. Even if it tests the file rather than parts(functions, methods...) as would be normal for a unittest.

It does not need to be a unittest and I don't what to automatically convert the files I just want to monitor the py3 compliance of files in a unittest like manor. I can't seem to find any documentation or examples for this.

An example and/or documentation would be great.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Vincent
  • 1,579
  • 4
  • 23
  • 38

2 Answers2

2

Simply use the -3 option with python2.6+ to be informed of Python3 compliance.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • But I am not sure how I would run this as a unittest. I want run as a test not run the code – Vincent May 10 '10 at 04:20
  • @Vincent, you'd have to run code to unit test, anyway, so why not simply add the "-3" option to ensure that it is Python3 compatible? What do you need it to do? – Michael Aaron Safyan May 10 '10 at 04:22
  • So what about when you're running code in a framework, where you have no control over how the python interpreter is actually run? (ex: Google App Engine) – Adam Parkin Mar 05 '12 at 19:27
1

If you are trying to verify the code will work in Python 3.x, I would suggest a script that copies the source files to a new directory, runs 2to3 on them, then copies the unit tests to the directory and runs them.

This may seem slightly inelegant, but is consistent with the spirit of unit testing. You are making a series of assertions that you believe ought to be true about the external behavior of the code, regardless of implementation. If the converted code passes your unit tests, you can consider your code to support Python 3.

stw_dev
  • 842
  • 11
  • 14
  • 1
    "If the converted code passes your unit tests, you can consider your code to support Python 3." -- doesn't this presume that the unit tests themselves are valid Python 3? – Adam Parkin Mar 05 '12 at 20:52
  • I would say you should write your unit tests more carefully, but that's not helpful ;-) There might be unit tests expecting one response in Python 2 but should require a different response in 3. Nothing can ultimately replace inspecting your code with the Python 3 changes in mind... this was simply a method to test both paradigms using one automated system. – stw_dev Mar 07 '12 at 12:52