se2p / pynguin

The PYthoN General UnIt Test geNerator is a test-generation tool for Python
https://www.pynguin.eu
MIT License
1.22k stars 74 forks source link

Error for python 3.11 File "<frozen runpy>, line 198, in _ron_module_as_main #52

Closed fhg-isi closed 8 months ago

fhg-isi commented 8 months ago

If I try to use pynguin with following command and Python 3.11.1 on Windows:

pynguin --project-path ./src --output-path ./test --module-name example

I get following error:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main                                                                   
  File "<frozen runpy>", line 88, in _run_code                                                                              
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Scripts\pynguin.exe\__main__.py", line 4, in <module>               
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\__init__.py", line 9, in <module>         
    import pynguin.generator as gen                                                                                         
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\generator.py", line 31, in <module>       
    import pynguin.analyses.seeding as seeding  # pylint: disable=consider-using-from-import                                
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                              
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\analyses\seeding.py", line 25, in <module>
    import pynguin.ga.testcasechromosome as tcc                                                                             
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\ga\testcasechromosome.py", line 13, in <module>
    import pynguin.ga.chromosome as chrom
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\ga\chromosome.py", line 13, in <module>
    import pynguin.ga.computations as ff
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\ga\computations.py", line 17, in <module>
    from pynguin.testcase.execution import ExecutionTrace
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\testcase\execution.py", line 425, in <module>
    class ExecutionTracer:
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\site-packages\pynguin\testcase\execution.py", line 460, in ExecutionTracer
    Compare.IN: lambda val1, val2: (
    ^^^^^^^^^^
  File "C:\python_env\App\WinPython\python-3.11.1.amd64\Lib\enum.py", line 787, in __getattr__
    raise AttributeError(name) from None
AttributeError: IN

=> Could you please update pynguin to work with Python 3.11?

stephanlukasczyk commented 8 months ago

Updating Pynguin to Python 3.11 is considerable effort, because it would require to revisit and rework the instrumentation of Python's bytecode. Unfortunately, I currently do not have capacity to do this, although I am happy to accept pull requests for this.

Besides, I think that if I invest the time to support a newer Python version, I'd go with the current Python 3.12, using its new sys.monitoring API.

I hope you can understand this decision and use Pynguin on Python 3.10.

fhg-isi commented 8 months ago

Sure, many thanks for quick clarification and your efforts. For others looking for a unit test generation solution also see related SO question: https://stackoverflow.com/questions/77409726/ho-to-generate-unit-tests-for-python-in-pycharm-using-src-and-test-folder-stru

ghostbody commented 6 months ago

@stephanlukasczyk May this project help you to upgrade to python 3.12 ~

https://github.com/asottile/pyupgrade

stephanlukasczyk commented 6 months ago

@ghostbody I know about pyupgrade, it's already being used as a pre-commit hook in Pynguin. However, migrating is not that easy. I am pretty sure that most of the code of Pynguin will run on Python 3.12 without any changes. But I know one, central, module that requires changes; changes that pyupgrade will not be helpful: the instrumentation of the Python bytecode to measure coverage and the like. It changes the bytecode of Python directly and the bytecode is an implementation detail of the CPython interpreter (one of the reasons why Pynguin does not run on other interpreters, e.g., PyPy), which changes with almost every version. I know of bytecode changes between Python 3.10, 3.11, and 3.12. These need to be addressed manually, for which I currently do not have time (though I am happy to accept pull requests).

Besides, for Python 3.12 and the future it might be wise to use the aforementioned sys.monitoring API instead of manipulating the bytecode directly. My hope would be that such an approach could be more stable and also would work in Python 3.13 without any changes. Again, I am happy to accept pull requests for this, but I cannot do it by myself currently. Finishing my PhD first is required :wink: