如何一个接一个地在套件中提供测试类?

时间:2018-05-22 10:31:30

标签: python python-3.x selenium automation

我已经编写了一个测试套件,一个接一个地执行我的所有测试类。 但问题是,在执行时,它会从类中获取方法并立即执行。

所以我想写一个代码,其中一个类中的所有方法都将被执行 然后它将进入下一个测试类。

测试类代码

from pages.Home.hamburger_page import HamburgerPage
from utilites.testStatus import TestStatus
import pytest
import unittest


@pytest.mark.usefixtures("oneTimeSetUp", "setUp")
class hamburgerTest(unittest.TestCase):

@pytest.fixture(autouse=True)
def classSetup(self, oneTimeSetUp):
    self.ha = HamburgerPage(self.driver)
    self.ts = TestStatus(self.driver)

@pytest.mark.run(order=1)
def test_hamburger_menu_latest_WAF021(self):
    result = self.ha.find_latest()
    self.ts.markFinal("To find latest link", result, "Click on latest link")

@pytest.mark.run(order=2)
def test_hamburger_menu_top__WAF022(self):
    result = self.ha.find_topics_links()
    self.ts.markFinal("To find top link", result, "Click on top link")

@pytest.mark.run(order=3)
def test_hamburger_menu_badges__WAF023(self):
    result = self.ha.find_badges()
    self.ts.markFinal("To find badges link", result, "Click on badges link")

@pytest.mark.run(order=4)
def test_hamburger_menu_users__WAF024(self):
    result = self.ha.find_users()
    self.ts.markFinal("To find user link ", result,"Click on users link")

@pytest.mark.run(order=5)
def test_hamburger_menu_groups__WAF025(self):
    result = self.ha.find_groups()
    self.ts.markFinal("To find groups link", result, "Click on groups link")

@pytest.mark.run(order=6)
def test_hamburger_menu_announcements__WAF026(self):
    result = self.ha.find_announcements()
    self.ts.markFinal("To find announcements link", result, "Click on announcements link")

第二次测试

from pages.Home.topic_page import topicPage
from utilites.testStatus import TestStatus
import pytest
import unittest
import time

@pytest.mark.usefixtures("oneTimeSetUp","setUp")
class TopicTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def classSetup(self,oneTimeSetUp):
    self.tp = topicPage(self.driver)
    self.ts = TestStatus(self.driver)

@pytest.mark.run(order=1)
def test_header_login(self):
    self.tp.find_header_logIn()

@pytest.mark.run(order=2)
def test_validLogin(self):
    self.tp.getelements("xxxxxx@xxx.com", "xxxxx")
    time.sleep(3)

@pytest.mark.run(order=3)
def test_FirstLinkInAnnouncements(self):
    self.tp.find_first_announcement_link()
    time.sleep(3)

@pytest.mark.run(order=5)
def test_TitleVerfication(self):
    self.tp.find_title()
    time.sleep(5)

@pytest.mark.run(order=4)
def test_ReplyDropDown(self):
    self.tp.find_scrollupfull()
    time.sleep(4)
    self.tp.find_topic_page_reply()

测试套件

import unittest
from tests.Home.hamburger_test import hamburgerTest
from tests.Home.topic_test import TopicTest

# Get all tests from the test classes
tc1 = unittest.TestLoader().loadTestsFromTestCase(hamburgerTest)
tc2 = unittest.TestLoader().loadTestsFromTestCase(TopicTest)

# Create a test suite combining all test classes

Test1 = unittest.TestSuite([tc1, tc2])


unittest.TextTestRunner(verbosity=2).run(Test1)

2 个答案:

答案 0 :(得分:0)

你应该分开执行tc1和tc2。例如。

Test1 = unittest.TestSuite([tc1])
Test2 = unittest.TestSuite([tc2])

unittest.TextTestRunner(verbosity=2).run(Test1)
unittest.TextTestRunner(verbosity=2).run(Test2)

希望这能解决您的问题。

答案 1 :(得分:0)

您正在使用pytest-ordering插件(https://pytest-ordering.readthedocs.io/en/develop/)。 您正在设置应执行测试的顺序。它使pytest执行测试从最高阶(1)到最低阶。可以在两个类中找到具有相同顺序的测试,这就是pytest在类之间跳跃的原因。