Blog Post #3

Utkarsh
Published: 07/04/2021

This blog covers the work done by me in the 3ed week of my GSOC experience. My work during this time was focused on fixing version issues that were causing 11 tests to fail on python 2 environment, but not on python 3. All the work can be found in this PR.

What did I do?

The majority of my work was to find a solution to the issues that were causing different behaviors on two different environments, i.e. Python 2 and Python 3. The test scripts that were giving problems were:-
  • test_wsaa.py
  • test_wsbfev1.py
  • test_wsfev1.py
  • test_wsbfexv1.py
  • test_wslsp.py
  • test_wsltv.py
  • test_wslum.py
  • test_wsmtx.py

The main problem was the way in which string is treated on python 2 vs python 3. The str object in Python 3 is quite similar but not identical to the Python 2 unicode object. The major difference is the stricter type-checking of Py3’s str that enforces a distinction between unicode strings and byte-strings, such as when comparing, concatenating, joining, or replacing parts of strings.
Along with this I also added a pytest configuration file to register custom markers that I used to stop using fixtures in particular scripts and test methods.

Did I face any problems?

Yes, I faced a lot of problems finding the particular errors that were causing the test failures. Also I trouble finding the exact solution to the problem otherwise a different solution would lead to tests failing on python 3 environment as well. Apart from all this, my other point of focus was to make my solution robust. For example, at first I decided to handle the tests for different versions differently in the test method itself like this
if sys.version_info[0] == 3:
        assert isinstance(wsbfev1.CAE,str)
    elif sys.version_info[0] == 2:
        assert isinstance((wsbfev1.CAE),future.types.newstr)
But then it was correctly pointed out by my mentors was that this made the code look ugly was not a robust solution. Therefore I found a different solution. future module contains a newstr type that is a backport of the str object from Python 3. This inherits from the Python 2 unicode class but has customizations to improve compatibility with Python 3’s str object. We can use it as follows:
>>> from __future__ import unicode_literals
>>> from builtins import str
Using str in this way eliminated the error and was also a robust solution.

Results

Using the above mentioned solution fixed the 11 tests that were not working on python 2 environment to run and pass smoothly. With this 202 tests now passed on both the environments.

Python 2

$ pytest
============================= test session starts =============================
platform win32 -- Python 2.7.12, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
rootdir: C:\Users\shiva\OneDrive\Desktop\Pyafipaws_Utkarsh\pyafipws, inifile: pytest.ini
plugins: html-1.22.1, metadata-1.11.0, vcr-1.0.2
collected 236 items

tests\test_ws_sr_padron.py s.sx                                          [  1%]
tests\test_wsaa.py ...X.                                                 [  3%]
tests\test_wsaa_crypto.py ....                                           [  5%]
tests\test_wsbfev1.py ..........                                         [  9%]
tests\test_wscdc.py s.......                                             [ 13%]
tests\test_wsct.py ..........xx..xxxxxxxxxxssxxxxxx                      [ 26%]
tests\test_wsfev1.py ...xxx                                              [ 29%]
tests\test_wsfev1_dummy.py .                                             [ 29%]
tests\test_wsfexv1.py ..........                                         [ 33%]
tests\test_wslsp.py ......................................x.             [ 50%]
tests\test_wsltv.py ............................x.                       [ 63%]
tests\test_wslum.py ........................x.                           [ 74%]
tests\test_wsmtx.py ..............x......x..........x                    [ 88%]
tests\test_wsremcarne.py ...........................                     [100%]

== 202 passed, 5 skipped, 28 xfailed, 1 xpassed, 1 warnings in 27.46 seconds ==

Python 3

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.9.2, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: C:\Users\shiva\OneDrive\Desktop\Pyafipaws_Utkarsh\pyafipws, configfile: pytest.ini
plugins: cov-2.12.1, html-3.1.1, metadata-1.11.0, vcr-1.0.2
collected 236 items

tests\test_ws_sr_padron.py s.sx                                          [  1%]
tests\test_wsaa.py ...X.                                                 [  3%]
tests\test_wsaa_crypto.py ....                                           [  5%]
tests\test_wsbfev1.py ..........                                         [  9%]
tests\test_wscdc.py s.......                                             [ 13%]
tests\test_wsct.py ..........xx..xxxxxxxxxxssxxxxxx                      [ 26%]
tests\test_wsfev1.py ...xxx                                              [ 29%]
tests\test_wsfev1_dummy.py .                                             [ 29%]
tests\test_wsfexv1.py ..........                                         [ 33%]
tests\test_wslsp.py ......................................x.             [ 50%]
tests\test_wsltv.py ............................x.                       [ 63%]
tests\test_wslum.py ........................x.                           [ 74%]
tests\test_wsmtx.py ..............x......x..........x                    [ 88%]
tests\test_wsremcarne.py ...........................                     [100%]

== 202 passed, 5 skipped, 28 xfailed, 1 xpassed, 1 warnings in 27.46 seconds ==