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

Implemented mock support #53

Closed
wants to merge 2 commits into from
Closed

Implemented mock support #53

wants to merge 2 commits into from

Conversation

dmytrostriletskyi
Copy link

@dmytrostriletskyi dmytrostriletskyi commented Jul 30, 2017

Hello! I frequency use ddt and mock together in my development.
I always missed some features in testing, that can make me feel more comfortable.

What is new?

I implemented decorator mockdata, that may be uses additional to common data and unpack.
This decorator says, that arguments in data values may be mock's patch object and related to it (after process it become MagicMock) attributes like return_value and side_effect (optional).

Usage

Simple example with arguments.

@data(
    [patch('application.utils.requests.get'), '/api/registration/', 'get'],
    [patch('application.utils.requests.post'), '/api/registration/', 'post']
)
@unpack
@mockdata
def test_registration_few_types(self, mock_request, url, http_request_type):
    register_user(http_request_type)
    mock_request.assert_called_with(url)

Another one.

@data(
    patch('application.utils.registration'), 
    patch('application.utils.authorization')
    patch('application.utils.send_statistic')
)
@unpack
@mockdata
def test_api_flow(self, mock_api_endpoint_func):
    make_api_flow()
    mock_api_endpoint_func.assert_called()

Example, where you can see, that Mock applied side_effect in background and you get result right away without applying it by yourself.

@data(
    {'patch': patch('application.utils.get_publication'), 'side_effect': KeyError()},
)
@unpack
@mockdata
def test_no_publication(self, mock_get_publication):
    result = get_publication('California')
    self.assertEqual('No publications yet.', result)

@dmytrostriletskyi dmytrostriletskyi changed the title Implemented mock suppor Implemented mock support Jul 30, 2017
@codecov-io
Copy link

codecov-io commented Jul 30, 2017

Codecov Report

Merging #53 into master will decrease coverage by 2.17%.
The diff coverage is 90.62%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #53      +/-   ##
==========================================
- Coverage     100%   97.82%   -2.18%     
==========================================
  Files           1        1              
  Lines         107      138      +31     
==========================================
+ Hits          107      135      +28     
- Misses          0        3       +3
Impacted Files Coverage Δ
ddt.py 97.82% <90.62%> (-2.18%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5e81603...307a58c. Read the comment docs.

@txels
Copy link
Collaborator

txels commented Oct 7, 2017

Hey thanks for this!
Apologies for the super-late review, I'm not sure how this PR escaped my radar for so long.

Without having yet looked into the details, this seems like a good contribution. Would you mind updating the documentation as well? I will review the implementation details.

@txels
Copy link
Collaborator

txels commented Oct 7, 2017

Have you considered using mockdata as a replacement for data (as we do with file_data) rather than an annotation-style decorator? I think it might be a bit more consistent with how things are done at the moment. What do you see as pros and cons of that approach?

@dmytrostriletskyi
Copy link
Author

@txels, heyo!

I think you are rigth with file_data approach for mock instead off additional @mock decorator to @data. Sorry, that I did not use to investigate package better to provide suitable way at the very beginning.

Let me time to refactor my stuff: I need remember ddt behave, create pretty code to corresponding approach (mock_data) and tests.

Thanks you a lot for payin attention on my pull request.

@wswld
Copy link
Contributor

wswld commented Apr 11, 2018

@dmytrostriletskyi Hi, are you still interested in this?

@wswld wswld mentioned this pull request Jun 25, 2018
@wswld
Copy link
Contributor

wswld commented Jun 25, 2018

Continuing this effort in another PR to fix and clean up some stuff. Closing this.

@wswld wswld closed this Jun 25, 2018
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

Successfully merging this pull request may close these issues.

4 participants