-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Allow methods to access a class fixture. #3359
Comments
GitMate.io thinks possibly related issues are #2938 (fixture with scope “class” running for every method), #2145 (Session scoped fixtures can leak out of a class), #2392 (Allow a fixture to be reset on a given condition), #1376 (allow fixtures to provide extra info when a test fails), and #2947 (Marking subclass affected methods of parent class.). |
if |
Agree with @brianmaissy if I'm understanding the question correctly.
How is that possible if |
I should have been more specific, Old Login method.
New Login Method
Because I have my login method requesting fixtures, its signature changes as I add more fixture dependencies to it. I use fixtures because theyve been convenient when setting up tests. Need a reference to the browser? I used a fixture. Need to know what environment your in (alpha, gamma, etc.)? Use a fixture. @brianmaissy If i understand correctly, I would have:
I think my problem im encountering is that Im depending on fixtures and trying to leverage pytest way more than I should be. |
Hi @Drew-Ack, I will assume you meant your code to be this: class Login:
@staticmethod
def login(browser):
# login stuff because of the
Not sure I follow, in pytest only test functions can "request" fixtures (actually their parameters are considered fixtures which pytest injects when it calls the test functions).
It is not correct to make your test framework drive your application, that's not what a test framework is meant to do. How do you run your application, by executing |
this is potentially just driving acceptance tests at the whole system level (at least i hope so) |
I should have been even more specific in my layout of the test project.
@nicoddemus You are correct in saying that my login function in my Login class does not request a fixture. I do however have the test functions request fixtures and pass them into the login functions as arguments, That is why their signature was growing anytime I needed to extend their functionality. What it used to be.The following code is what i think is relevant from the entire project. I can include more if neccessary. Login.py
test_login.py
conftest.py
My largest issue came when we started adding options to switch environments; alpha, gamma, etc.
And it worked, but to allow my login function from my Login class to be able to access this session level object, i now had to increase the login function signature to include login_url.
What it now isSo after looking at that and thinking to myself, nooooo, there has to be a better way, I decided to look at what hole ive gone down and see how to make this better. The following is the current way. conftest.pyI now have a fixture at session level that requests the browser and login_url fixtures. It instantiates a Login object that now test functions can request and use.
Login.py
test_login.py
I now have a "cleaner" way of expanding my login object via fixtures. Now i dont use fixtures for everything, but it does seem the easiest way to go when im passing around common things my tests need to run. My Login class does depend on fixtures, and im not sure if thats good or bad. After explaining this though, it does seem kind of messed up. Am i off the wagon here? @RonnyPfannschmidt am i doing this completely wrong? Yay junior level development. :D @nicoddemus To currently run my project I navigate to the root folder of my project and I type: |
Thanks for expanding the explanation @Drew-Ack, being verbose and explaining things thoroughly goes a long way to help others understand your problem without wasting time trying to guess what's going on. 😉 So if I understand your Being part of the test suite, is perfectly fine that your One of the benefits of fixtures is that they abstract away what your resources require to be created, which is exactly the problem you encountered yourself: previously all your tests created a def test_foo(browser):
Login.login(browser) And so on for dozens of tests. This doesn't abstract away the signature of the You figured out the proper solution, you should change your login process into a fixture, that way tests don't need to care about what def test_foo(login):
login.login() In summary, one (among many) benefits of fixtures is that they abstract away what the fixture resource needs to function, so if that changes in the future your tests remain unaffected, only the fixture code needs to be touched. That's invaluable and a point that many people seem to miss. Cool that you figured out the solution yourself. 😎 I'm closing this now, but feel free to follow with the discussion. 👍 |
I have a class I use for logging in :
browser
is a fixture.login_url
is a NEW fixture.When ever I called login in other scripts, it had the signature of
Login.login(browser)
Well, adding the fixture
login_url
as a new additional parameter to login, has changed the signature toLogin.login(browser,login_url)
. This breaks every single call to everytime i call the login method because the function signature has changed.My problem is there has to be a better way to have functions within the
Login
class to have access to the url thatlogin_url
provides without having to be so explicit in the method signatures within theLogin
class.I suppose I want a fixture that operates at the class level and exposes a value that the functions can pick up and go on their marry way.
So after scouring the webs for solutions I come to the developers and maintainers for assistance.
Ive looked into; autouse, parameterization, and marking at class level. I cant get any of them to do what I need, but I also might be using them wrong.
The text was updated successfully, but these errors were encountered: