About the mock in KUnit

31 views
Skip to first unread message

ddakjoa

unread,
Oct 26, 2021, 5:24:25 AM10/26/21
to kuni...@googlegroups.com

Dear sir,

 

I have a few questions about KUnit, so I'm sending you an email. 

I recently looked through the KUnit related documentation and sources of kernel-5.14, but couldn't find anything about Mock related APIs.

And it was confirmed that the following text was also found in the API item in the document.

"This file documents all of the standard testing API excluding mocking or mocking related features."

 

So my question is:

1. Where can I find the definition of mock-related API and how to use it?

 

 

Thank you

Best Regards

Daniel Latypov

unread,
Oct 26, 2021, 11:40:19 AM10/26/21
to ddakjoa, kuni...@googlegroups.com
On Tue, Oct 26, 2021 at 2:24 AM ddakjoa <soos...@naver.com> wrote:

Dear sir,

 

I have a few questions about KUnit, so I'm sending you an email. 

I recently looked through the KUnit related documentation and sources of kernel-5.14, but couldn't find anything about Mock related APIs.

And it was confirmed that the following text was also found in the API item in the document.

"This file documents all of the standard testing API excluding mocking or mocking related features."


I'm sorry, that statement in the documentation is incorrect.
I've been meaning to update that sentence but kept forgetting. I've sent a patch out to fix that.
(The history is that the original patchset included mocking, but it was removed and the documentation patches weren't updated).

 

So my question is:

1. Where can I find the definition of mock-related API and how to use it?


There is no API at the moment.

You'll need to implement your own mocking mechanism for now.
https://kunit.dev/mocking is currently the best resource we have with tips on how to do that.

Note: if possible, one should prefer fakes instead of mocks, see https://kunit.dev/mocking#fakes-versus-mocks

 

 

Thank you

Best Regards

--
You received this message because you are subscribed to the Google Groups "KUnit Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/cd3b5b3667a2651849ea365b87a8e6d5%40cweb008.nm.nfra.io.

Daniel Latypov

unread,
Oct 26, 2021, 12:13:33 PM10/26/21
to ddakjoa, kuni...@googlegroups.com

On Tue, Oct 26, 2021 at 9:03 AM ddakjoa <soos...@naver.com> wrote:

Thank you for your quick reply.

 

I'm sorry, but I have one more question.

 

According to the answer, you said I should use fakes instead of mocks.


Sorry, I should have been more clear.
We'd encourage people to use fakes since those will generally lead to more maintainable tests.
But in some cases, it's far easier and better to use mocks and that sounds like it's the case for you.
 

Actually, I'm trying to white-box test the device drivers(e.g. I2C, SPI, UART, etc.) using KUnit.

However, in the case of device driver, there are many parts that are processed by receving the return value from the hardware.

If google test is used, the hardware-dependent functions can be tested as mock_method(), 

but in the case of KUnit, if mock cannot used, wouldn't it be impossible to write test code for hardware-dependent fuctions?

It's possible, but you'd have to first figure out how you want to intercept the function calls.
MOCK_METHOD() in googletest works by using virtual methods, so it can't stub out a generic free function for you, and it adds runtime overhead.

The problem faced in KUnit is there's no generic approach that
1. has no runtime overhead
2. doesn't have readability/maintainability downsides
So that's why it lacks any first part support for doing this for you.

https://kunit.dev/mocking.html#function-redirection is the relevant section here.

If you're fine with the indirect function call overhead like MOCK_METHOD() would have then you could create an ops struct and use that.
(https://kunit.dev/mocking.html#run-time-ops-structs-class-mocking)

If you want to avoid that, you can make a __weak wrapper function that you can replace at link-time
(https://kunit.dev/mocking.html#hybrid-approaches-limiting-scope)

I'm not sure if it would make sense to use a fake for this.

In fact, I'm not sure if it is possible to use fakes to replace hardware-dependent function calls in code like google test;s mocks.

 

Please advise on this.

 

Thank you

Best Regards

 

 

 

 

-----Original Message-----
From: "Daniel Latypov"<dlat...@google.com>
To: "ddakjoa"<soos...@naver.com>;
Cc: <kuni...@googlegroups.com>;
Sent: 2021-10-27 (수) 00:40:05 (GMT+09:00)
Subject: Re: About the mock in KUnit
 

On Tue, Oct 26, 2021 at 2:24 AM ddakjoa <soos...@naver.com> wrote: 

Dear sir,

 

I have a few questions about KUnit, so I'm sending you an email. 

I recently looked through the KUnit related documentation and sources of kernel-5.14, but couldn't find anything about Mock related APIs.

And it was confirmed that the following text was also found in the API item in the document.

"This file documents all of the standard testing API excluding mocking or mocking related features."


I'm sorry, that statement in the documentation is incorrect.
I've been meaning to update that sentence but kept forgetting. I've sent a patch out to fix that.
(The history is that the original patchset included mocking, but it was removed and the documentation patches weren't updated).
 

 

So my question is:

1. Where can I find the definition of mock-related API and how to use it?


There is no API at the moment.

You'll need to implement your own mocking mechanism for now.
https://kunit.dev/mocking is currently the best resource we have with tips on how to do that.

Note: if possible, one should prefer fakes instead of mocks, see https://kunit.dev/mocking#fakes-versus-mocks 

 

 

Thank you

Best Regards

--
You received this message because you are subscribed to the Google Groups "KUnit Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/cd3b5b3667a2651849ea365b87a8e6d5%40cweb008.nm.nfra.io.
Reply all
Reply to author
Forward
0 new messages