Pytest - 运行 来自单个文件的多个测试
Pytest - run multiple tests from a single file
我正在使用 Pytest (Selenium) 来执行我的功能测试。我将测试分为以下结构中的 2 个文件:
My_Positive_Tests.py
class Positive_tests:
def test_pos_1():
populate_page()
check_values()
def test_pos_2():
process_entry()
validate_result()
My_Negative_Tests.py
class Negative_tests:
def test_neg_1
populate_page()
validate_error()
断言在函数内完成 (check_values、validate_result、validate_error)。
我正在尝试找到一种方法 运行 来自单个文件的所有测试,以便主测试文件看起来像:
My_Test_Suite.py
test_pos_1()
test_pos_2()
test_neg_1()
然后从命令行执行:
py.test --tb=short "C:\PycharmProjects\My_Project\MyTest_Suite.py" --html=report.html
有没有可能做这样的事情?我一直在搜索,但未能找到如何将对这些测试的调用放在一个文件中。
您不必 运行 手动进行测试。 Pytest finds and executes them automatically:
# tests/test_positive.py
def test_pos_1():
populate_page()
check_values()
def test_pos_2():
process_entry()
validate_result()
和
# tests/test_negative.py
def test_neg_1():
populate_page()
validate_error()
如果你那么运行
py.test
它们应该会被自动拾取。
然后您还可以 select 单个文件
py.test -k tests/test_negative.py
或单测
py.test -k test_neg_1
您需要更改 class 名称
class Positive_tests: to--> class Test_Positive:
class 名称也应以 "Test" 开头,以便 Pytest 发现。如果你不想在你的 class 名字前使用 "Test" 你也可以配置它,官方文档 here
文件名和测试以 test_
开始或以 _test.py
结束。 类 应该以 Test
开头
目录示例:
|-- 文件
|--|-- stuff_in 东西
|--|--|-- blah.py
|--|-- example.py
|
|-- 测试
|--|-- stuff_in 东西
|--|--|-- test_blah.py
|--|-- test_example.py
在终端中:$ py.test --cov=files/ tests/
或者只是 $ py.test tests/
如果您不需要代码覆盖率。测试目录文件路径必须在您当前的目录路径中。或者确切的文件路径 ofcourse
使用上述终端命令($ py.test tests/
),pytest 将在tests/ 目录中搜索所有以test_
开头的文件。文件也是如此。
test_example.py
# imports here
def test_try_this():
assert 1 == 1
# or
class TestThis:
assert 1 == 0
# or even:
def what_test():
assert True
我正在使用 Pytest (Selenium) 来执行我的功能测试。我将测试分为以下结构中的 2 个文件:
My_Positive_Tests.py
class Positive_tests:
def test_pos_1():
populate_page()
check_values()
def test_pos_2():
process_entry()
validate_result()
My_Negative_Tests.py
class Negative_tests:
def test_neg_1
populate_page()
validate_error()
断言在函数内完成 (check_values、validate_result、validate_error)。 我正在尝试找到一种方法 运行 来自单个文件的所有测试,以便主测试文件看起来像:
My_Test_Suite.py
test_pos_1()
test_pos_2()
test_neg_1()
然后从命令行执行:
py.test --tb=short "C:\PycharmProjects\My_Project\MyTest_Suite.py" --html=report.html
有没有可能做这样的事情?我一直在搜索,但未能找到如何将对这些测试的调用放在一个文件中。
您不必 运行 手动进行测试。 Pytest finds and executes them automatically:
# tests/test_positive.py
def test_pos_1():
populate_page()
check_values()
def test_pos_2():
process_entry()
validate_result()
和
# tests/test_negative.py
def test_neg_1():
populate_page()
validate_error()
如果你那么运行
py.test
它们应该会被自动拾取。
然后您还可以 select 单个文件
py.test -k tests/test_negative.py
或单测
py.test -k test_neg_1
您需要更改 class 名称
class Positive_tests: to--> class Test_Positive:
class 名称也应以 "Test" 开头,以便 Pytest 发现。如果你不想在你的 class 名字前使用 "Test" 你也可以配置它,官方文档 here
文件名和测试以 test_
开始或以 _test.py
结束。 类 应该以 Test
开头
目录示例:
|-- 文件
|--|-- stuff_in 东西
|--|--|-- blah.py
|--|-- example.py
|
|-- 测试
|--|-- stuff_in 东西
|--|--|-- test_blah.py
|--|-- test_example.py
在终端中:$ py.test --cov=files/ tests/
或者只是 $ py.test tests/
如果您不需要代码覆盖率。测试目录文件路径必须在您当前的目录路径中。或者确切的文件路径 ofcourse
使用上述终端命令($ py.test tests/
),pytest 将在tests/ 目录中搜索所有以test_
开头的文件。文件也是如此。
test_example.py
# imports here
def test_try_this():
assert 1 == 1
# or
class TestThis:
assert 1 == 0
# or even:
def what_test():
assert True