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

How to use with setUpClass() #752

Closed
buhtz opened this issue Nov 26, 2022 · 7 comments
Closed

How to use with setUpClass() #752

buhtz opened this issue Nov 26, 2022 · 7 comments

Comments

@buhtz
Copy link

buhtz commented Nov 26, 2022

From the documentation it is not clear how I can use PyFakeFS in a setUpClass().

setuppyfakefs() only works with a self instance from a setUp() method.

My current solution is to use a Patcher object in setUpClass() like this:

def My(unittest.TestCase):
    @classmethod
    def tearDownClass(cls):
        cls._patcher.tearDown()

    @classmethod
    def setUpClass(cls):
        cls._patcher = pyfakefs_ut.Patcher()
        cls._patcher.setUp()

Maybe you know a better way?

You could improve the "Usage" section of the docu with an situation using setUpClass().

My use-case:
I do create some testfiles in the fake filesystem. Because I only want to read them in the tests I need to create them only once for all tests. Creating them for each test again and again is ressourc inefficient.

@mrbean-bremen
Copy link
Member

No, there is no specific support for that. It is assumed that the setup is fast enough to happen in each test, and so far, this has not been an issue.
Your implementation is fine as is, and I'm not sure if we need more than that, given that this is the first time this use case ever came up. Supporting this specifically would mean just a thin wrapper around that implementation, and I'm not sure if it's worth it.
You are right about the documentation - we could that use case to the usage section.

@mrbean-bremen
Copy link
Member

Actually, on a second thought, it makes probably sense to add a class method for setup. As I recently changed the implementation to allow only one patched instance (this was made for session or module scoped fixtures in pytest), this will allow to access the fake filesystem via self.fs in the tests as usual.

@mrbean-bremen
Copy link
Member

I had another look at this, and while I can add a convenience function for class setup (I did this locally), it may not behave as expected. It would ehave the same way as creating the patcher in the class setup, as shown in the original comment. That would also mean that any changes to the file system in a single test would persist, so the test would influence each other.
Ideally, we would need to capture the state at test begin and reset the file system to that after test end, but I'm not sure how feasible this would be.
@buhtz, what do you think?

@buhtz
Copy link
Author

buhtz commented Dec 4, 2022

That would also mean that any changes to the file system in a single test would persist, so the test would influence each other. Ideally, we would need to capture the state at test begin and reset the file system to that after test end, but I'm not sure how feasible this would be. @buhtz, what do you think?

😄 I think that you think way to far. 😀 I'm aware that the each test can (theoretically) influence each other. But a developer who write unittest should know that. It is his/her responsibility and not yours. You can give a note about that in your documentation but that is IMHO enough. I see definitely no need for a reset- or snapshot-feature.

In my case all unittests only reading from the file system. Thats all. They don't write to it. So I see no problem.

@mrbean-bremen
Copy link
Member

Thanks!

I think that you think way to far

Well, if you say so 😀
Ok, I'll probably just add that convenience class setup method (and a class scoped fixture for pytest) in this case, and make the behavior clear in the documentation.

@mrbean-bremen
Copy link
Member

I added the class method setUpClassPyfakefs that shall do what you want in main - it took me longer than anticipated.
Please check if this works for you!

@buhtz
Copy link
Author

buhtz commented Dec 9, 2022

Dear @mrbean-bremen ,
thanks a lot for investing time into this.

I can confirm that #758 does work. You can see it in real world here.

@buhtz buhtz closed this as completed Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants