21 May 2024 Leave a comment Tech-Help
When using Pytest with Selenium, encountering the message “no tests ran” can be frustrating, especially when you believe your tests are correctly configured. This article will guide you through common solutions to ensure your tests are recognized and executed by Pytest.
Understanding Pytest Conventions
Pytest has specific naming conventions for test discovery:
- Test classes must start with the word
Test
. - Test function names must start with the prefix
test_
. - Test files should be named as
test_*.py
or*_test.py
.
Ensuring your test class and function names adhere to these conventions is crucial for Pytest to discover and run your tests.
Example: Correcting Your Test Class
Consider the following example where the test class doesn’t follow the naming convention:
import pytest
from selenium import webdriver
from pages import *
from locators import *
from selenium.webdriver.common.by import By
import time
class RegisterNewInstructor:
def setup_class(cls):
cls.driver = webdriver.Firefox()
cls.driver.get("http://mytest.com")
def test_01_clickBecomeTopButtom(self):
page = HomePage(self.driver)
page.click_become_top_button()
self.assertTrue(page.check_instructor_form_page_loaded())
def teardown_class(cls):
cls.driver.close()
To ensure Pytest picks up the class, rename it to:
import pytest
from selenium import webdriver
from pages import *
from locators import *
from selenium.webdriver.common.by import By
import time
class TestRegisterNewInstructor:
def setup_class(cls):
cls.driver = webdriver.Firefox()
cls.driver.get("http://mytest.com")
def test_01_clickBecomeTopButtom(self):
page = HomePage(self.driver)
page.click_become_top_button()
self.assertTrue(page.check_instructor_form_page_loaded())
def teardown_class(cls):
cls.driver.close()
Additional Tips
- Ensure your test file name matches the pattern
test_*.py
or*_test.py
. - Consider adding
@classmethod
decorator tosetup_class
andteardown_class
methods. - Check your
pytest.ini
configuration file for any custom settings that might interfere with test discovery.
Practical Example
If your test file is named my_test_ex1.py
and located in a directory like my_tests/
, ensure your pytest.ini
file includes:
[pytest]
python_files = my_test_*.py
This setting tells Pytest to look for test files matching the pattern my_test_*.py
.
Streamline Your Testing with Repeato
While configuring tests manually can be challenging, using a no-code test automation tool like Repeato can significantly simplify the process. Repeato allows you to create, run, and maintain automated tests for your iOS and Android apps without writing code. With its intuitive test recorder and computer vision-based automation, you can quickly set up and execute tests. Repeato also supports scripting for advanced use cases, making it a versatile solution for both beginners and experienced testers.
For more information on how Repeato can enhance your testing workflow, visit our documentation page.