I am playing around with a new web service and thought at the same time that I was writing some proof of concept code I would practice writing them as unit tests. So I wrote a test class that authenticates and then looks at the result (a session object) to see if the authentication was successful. That test is its own class and there are a couple other authentication methods that it tests. That works just fine. But now if I want to actually use that session object later in other test classes what is the best practice way of doing this.
I guess I could make the session object a singleton on the authentication test class. Then pull it from there. I could re-authenticate each time. Even with both of those I want to be sure the authentication test has run each time. I did fine the @AfterClass annotation in the testng docs but that does not apply to classes. I think its to be used only one methods to make them run after the rest of the tests in the class have run. I cant think of a good way to do this that does not feel like a little hack. What approach would the more experienced testng users suggest?
- Thanks
If you’re using the same class, simply store the token in a field.
If you are using different classes, use ITestContext#{getAttribute,setAttribute} to share data among your tests (here is how to do that).