When I ran this code in VS Code, it gives msg that "no testcase ran in 0.2s". So, How can I ran testcases in VS Code.
import pytest
from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
# from testcases.utilities.BaseClass import BaseClass
@pytest.mark.usefixtures("setup")
class Login():
def test_login(self, setup):
setup.driver.get("https://www.google.com/")
sleep(5)```
2
Answers
From the pytest documentation (emphasis mine):
"pytest discovers all tests following its Conventions for Python test discovery, so it finds both test_ prefixed functions. There is no need to subclass anything, but make sure to prefix your class with Test otherwise the class will be skipped."
Just expanding on the good answer by @Anonymous, which follows the
pytest
documentation guidelines on test discovery conventions.Pytest
simplifies the process of running tests by automatically discovering test functions and classes that adhere to specific naming conventions. Specifically, forpytest
to recognise a test case within a class, the class name must start with Test. Your class name Login does not follow this convention, which is whypytest
did not run any tests when you attempted to execute your code.It is important to understand how
pytest
organises and runs tests. When grouping tests into classes withpytest
, the classes do not need to inherit from any special base class provided bypytest
This organisation can help in structuring tests logically, especially when dealing with larger test suites. For
pytest
to recognise and run tests within a class, the methods inside must be named appropriately, typically starting withtest_
. This naming convention signals topytest
which methods are test cases.Additionally, the
pytest
documentation elaborates on the benefits of grouping tests into classes, such as improved test organisation, the ability to share fixtures across tests within the same class, and applying marks at the class level to affect all contained tests. However, a critical design choice inpytest
ensures that each test method within a class has a unique instance of the class. This approach promotes test isolation and discourages the sharing of state between tests, which is a best practice in software testing to avoid inter-test dependencies that can lead to fragile tests.Additionally,
pytest
offers powerful fixtures that can be used to set up and tear down test environments or provide resources needed for tests. These fixtures can be scoped and shared across multiple test functions and classes, further enhancing test organisation and reusability, one of the cornerstones of object oriented programming (inheritance, polymorphism and encapsulation being the others).Lastly, pytest’s automatic discovery and naming conventions are designed to make it easier for developers to write and run tests efficiently. It’s important to familiarise yourself with these conventions and the rationale behind them to effectively leverage
pytest
for your testing needs. Adhering to the naming conventions (TestClass
for classes andtest_
prefix for methods) ensures that your tests are discovered and executed as expected.For practical steps in VS Code, ensure you have the Python extension installed and configured for your project (I use the Microsoft one). Right-click on your test file in the Explorer or use the Command Palette to run tests. If tests are still not discovered, check your configuration settings for pytest in your
settings.json
file (or ensure thatpytest
is correctly installed and recognised in your project’s virtual environment).