Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python_test_example #6

Closed
suaaa7 opened this issue Jul 23, 2020 · 10 comments
Closed

python_test_example #6

suaaa7 opened this issue Jul 23, 2020 · 10 comments

Comments

@suaaa7
Copy link
Owner

suaaa7 commented Jul 23, 2020

Contents

@suaaa7
Copy link
Owner Author

suaaa7 commented Jul 25, 2020

$ python3 integration_test.py 
* Module setup
** Test setup
** Test 1
** Test clean-up
.** Test setup
** Test 2
** Test clean-up
.* Module clean-up

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

What is . in .** Test setup or .* Module clean-up'?


Tests have 3 possible outcomes:

  • ok: The test passes.
  • FAIL: The test does not pass, and raises an AssertionError exception.
  • ERROR: The test raises an exception other than AssertionError.

https://pymotw.com/2/unittest/

ok_fail_error.py

from unittest import TestCase, main

class OkFailErrorTestCase(TestCase):
    def test_ok(self):
        self.assertEqual(1, 1)

    def test_fail(self):
        self.assertEqual(1, 2)

    def test_error(self):
        import requests
        self.assertEqual(1, 1)

if __name__ == '__main__':
    main()
$ python3 ok_fail_error.py 
EF.
======================================================================
ERROR: test_error (__main__.OkFailErrorTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "ok_fail_error.py", line 5, in test_error
    import requests
ModuleNotFoundError: No module named 'requests'

======================================================================
FAIL: test_fail (__main__.OkFailErrorTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "ok_fail_error.py", line 12, in test_fail
    self.assertEqual(1, 2)
AssertionError: 1 != 2

----------------------------------------------------------------------
Ran 3 tests in 0.001s

FAILED (failures=1, errors=1)

@suaaa7
Copy link
Owner Author

suaaa7 commented Jul 25, 2020

$ python3
>>> from mock import Mock
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mock'
>>> from unittest.mock import Mock
>>> m = Mock()
>>> m
<Mock id='4449643664'>
>>> m()
<Mock name='mock()' id='4449643728'>
>>> m.return_value = 25
>>> m
<Mock id='4449643664'>
>>> m()
25
>>> m.a
<Mock name='mock.a' id='4449643728'>
>>> m.a = 10
>>> m.a
10
>>> m.a.return_value = 20
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'return_value'
>>> 

@suaaa7
Copy link
Owner Author

suaaa7 commented Jul 26, 2020

  • setUpModule or setUpClass?
    • setUpClass can't use self

@suaaa7
Copy link
Owner Author

suaaa7 commented Jul 30, 2020

ImportError: Failed to import test module:

@suaaa7
Copy link
Owner Author

suaaa7 commented Aug 1, 2020

root@ce3ecf514550:/opt# python3 -m unittest flask_app/app_test.py 
In service
Instantiate Service in app
Init Service
In app_test
Call setUp in AppTestCase
.Call setUp in AppTestCase
.Call setUp in AppTestCase
Call predict in app
Call predict in service
.Call setUp in AppTestCase
Call predict in app
Call predict in service
.Call setUp in AppTestCase
Call predict in app
Call predict in service
.
----------------------------------------------------------------------
Ran 5 tests in 0.023s

OK

@suaaa7
Copy link
Owner Author

suaaa7 commented Aug 1, 2020

  • Is it possible to solve ModuleNotFoundError with mock?
    • Maybe not
    • But, I can do what I want with patch
    def setUp(self):
        self.load_model_patcher = patch('flask_app.model.load_model')
        self.load_model_m = self.load_model_patcher.start()

        from flask_app.app import app
        self.client = app.test_client()

    def tearDown(self):
        self.load_model_patcher.stop()

@suaaa7
Copy link
Owner Author

suaaa7 commented Aug 2, 2020

  • I want to use both flask.Response and flask.jsonify
    • Use flask.make_response instead of flask.Response
from flask import Response, jsonify, make_response

@app.route('/')
def index():
    data = {'message': 'ok'}
    # return Response(response=jsonify(data), status=200)
    return make_response(jsonify(data), 200)

@suaaa7
Copy link
Owner Author

suaaa7 commented Aug 10, 2020

@suaaa7
Copy link
Owner Author

suaaa7 commented Aug 16, 2020

There are no .py[i] files in directory '.'
##[error]Process completed with exit code 2.
root@93442a991c37:/opt# mypy .
Success: no issues found in 14 source files
root@93442a991c37:/opt# cd ..
root@93442a991c37:/# mypy .
There are no .py[i] files in directory '.'
root@93442a991c37:/# mypy opt 
Success: no issues found in 14 source files
root@93442a991c37:/# mypy opt/ --config-file opt/mypy.ini
Success: no issues found in 14 source files
root@93442a991c37:/# mypy opt/ --config-file opt/mypy.ini
opt/mock/my_class_test.py:7: error: Function is missing a type annotation
opt/mock/my_class_test.py:9: error: Function is missing a type annotation
opt/mock/my_class_test.py:13: error: Function is missing a return type annotation
opt/mock/my_class_test.py:24: error: Function is missing a return type annotation
Found 4 errors in 1 file (checked 14 source files)

@suaaa7 suaaa7 mentioned this issue Aug 16, 2020
@suaaa7 suaaa7 closed this as completed Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant