Changeset 1746:72d797a82efe


Ignore:
Timestamp:
Dec 31, 2018, 11:05:52 PM (3 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Add test support class `ScriptedSession`

This code is intended as the replacement of (most of) the currently
used mock code. In the current mock code, multiple tests sometimes
share test data. Hence, a developer must be careful not to add a test
and make other tests fail as a side effect.

The basis of the new mock code is the class
`scripted_session.ScriptedSession`, which is used as (kind of) a
replacement for the session factory argument of `FTPHost`.

Since `ScriptedSession` needs a `script` parameter that common session
factories don't take, I added a helper function
`scripted_session.factory`, which takes a `script` and returns a
session factory that can be used in `FTPHost`.

With the new test code, I could rewrite some of the tests in
`test_host.py` rather nicely. I was able to remove the session class
`UnnormalizedCurrentWorkingDirectory`. However, I had to keep
`FailOnLoginSession`, but only because the class is still used in
another test module.

I'm not yet sure how well the new approach will work with the
remaining tests, but I'm about to find out. I couldn't determine
this just by looking at the test code.
Location:
test
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_host.py

    r1721 r1746  
    2121from test import mock_ftplib
    2222from test import test_base
     23import test.scripted_session as scripted_session
    2324
    2425
     
    7071        else:
    7172            raise ftplib.error_temp
    72 
    73 
    74 class UnnormalizedCurrentWorkingDirectory(mock_ftplib.MockSession):
    75 
    76     def pwd(self):
    77         # Deliberately return the current working directory with a
    78         # trailing slash to test if it's removed when stored in the
    79         # `FTPHost` instance.
    80         return "/home/"
    8173
    8274
     
    175167        expected.
    176168        """
    177         host = test_base.ftp_host_factory()
     169        script = [
     170          scripted_session.Call(method_name="__init__", result=None),
     171          scripted_session.Call(method_name="pwd", result="/"),
     172          scripted_session.Call(method_name="close")
     173        ]
     174        host = test_base.ftp_host_factory(scripted_session.factory(script))
    178175        host.close()
    179176        assert host.closed is True
     
    182179    def test_invalid_login(self):
    183180        """Login to invalid host must fail."""
     181        script = [
     182          scripted_session.Call(method_name="__init__", result=ftplib.error_perm),
     183          scripted_session.Call(method_name="pwd", result="/"),
     184        ]
    184185        with pytest.raises(ftputil.error.FTPOSError):
    185             test_base.ftp_host_factory(FailOnLoginSession)
     186            test_base.ftp_host_factory(scripted_session.factory(script))
    186187
    187188    def test_pwd_normalization(self):
     
    189190        Test if the stored current directory is normalized.
    190191        """
    191         host = test_base.ftp_host_factory(UnnormalizedCurrentWorkingDirectory)
     192        script = [
     193          scripted_session.Call(method_name="__init__", result=None),
     194          # Deliberately return the current working directory with a
     195          # trailing slash to test if it's removed when stored in the
     196          # `FTPHost` instance.
     197          scripted_session.Call(method_name="pwd", result="/home/")
     198        ]
     199        host = test_base.ftp_host_factory(scripted_session.factory(script))
    192200        assert host.getcwd() == "/home"
    193201
Note: See TracChangeset for help on using the changeset viewer.