python - compare generator objects within mock.assert_has_calls -
I am writing a unit test for a function which will confirm that the function to be said in it is called with the correct argument . The problem is that one of the arguments is the generator.
Is there a way to compare the generator content that is called using the fn ? An example of the behavior I see is 'AssertSequenceEqual.' As is the case, because test_use_fn fails because the generator objects that are comparing are different.
import fake Def Fn (entries): Pass Def_fn (entries, Convert = lambda x: x): Convert insertion entries = (entries) Entries = Fan (entries) Entries = FN (Entries) @mock Patch ( "FN") def Test_use_fn (self Mok_fan): mock_fn.return_value = 'Falcon' entries = [ 'Fu', 'bar'] Upyog_fan (entries) Call_l = Mokkcol (entry Log entries) ) Call_2 = Mokkol ('falcon') mock_fn.assert_has_calls ([call_1, Call_2]) < P> You can use call_args_list I think you will check whether each item in the generator is the same or not. I write a assertEqualGenerators () method of the test code class that works (if the argument is not there, the generators use the standard assertEqual () ). The module file name is mock_generators.py and you must use mock_generators.fn to patch fn . The last move is that the call is the object arguments: Take a look for the data of how to get the data (the first element in your case that you want).
entries = (Entry entry) (entries) entries = Fan (entries) Entries = Fan (entries)) class MyTestCase (unittest.TestCase) for admission to listing: DEF specified Akwlenjnaretrs (self, a, B): Try this: for x, y ZIP (a, B): self.assertEqual (x, y) except TypeError: SelfkassertEqual (a, B) @ Mockkpatch ( " mock_generators.fn ") def test_use_fn (self, mock_fn): mock_fn.return_value = 'baz' entries = [ 'foo', 'bar'] use_fn (entries) Call = [Mock.call (login for entries in the entry)), copy ('falcon')] self. Setakwl (Lane (calls), Mk_fn. Siel_count ZIP (Mk_fnkcol_arg_lait, calls) in) a, b: Self.assertEqualGenerators (a [0], b [0]) If __name__ == '__main__': unittest.main ( )
Comments
Post a Comment