0

The project has so many modules. There are functional test cases being written for almost every api written like for GET requests, POST requests and PUT requests. To test an individual file we use the syntact pytest tests/file_name.py but I want to test a specific method in that file. Is there any way to test it like that??

1 Answer 1

0

Duplicate of Is there a way to specify which pytest tests to run from a file?

In a few words, you can use the -k option of pytest to specify the name of the test you would like to run.

4
  • So in my case , there are some methods like test_login_user_successful(),test_login_user_unsuccessful(),test_refresh_successful(),get_username() in test_users.py file. I want to test get_username() method. How would that be tested
    – Sai Naveen
    Commented Aug 4, 2022 at 12:56
  • Have you tried: pytest tests/file_name.py -k 'test_get_username' ? I wonder if your issue isn't the missing prefix for your test. All pytest's tests MUST be preceded by 'test_' otherwise it's not concidered as a test.
    – Toadster
    Commented Aug 4, 2022 at 14:11
  • If your tests are enclosed in a test suite class, use pytest tests/file_name.py -k '<YourTestClass>.test_get_username'
    – Toadster
    Commented Aug 4, 2022 at 14:20
  • Yes issue is that I havent mentioned test as a prefix!! My bad!
    – Sai Naveen
    Commented Aug 5, 2022 at 5:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.