Best Python code snippet using lemoncheesecake
test_behave.py
Source:test_behave.py
...30@then("a + b is equal to {value:d}")31def step_impl(context, value):32 check_that("%s + %s" % (context.a, context.b), context.a + context.b, equal_to(value))33"""34 def run_behave_tests(tmpdir, feature_content, step_content, env_content=None, expected_report_dir=None):35 tmpdir.mkdir("features").join("feature.feature").write_text(feature_content, "utf-8")36 tmpdir.mkdir("steps").join("step.py").write_text(step_content, "utf-8")37 if not env_content:38 env_content = u"""from lemoncheesecake.bdd.behave import install_hooks39install_hooks()40"""41 tmpdir.join("environment.py").write_text(env_content, "utf-8")42 with change_dir(tmpdir.strpath):43 behave_main([osp.join("features", "feature.feature")])44 if not expected_report_dir:45 expected_report_dir = tmpdir.join("report").strpath46 return load_report(expected_report_dir)47 def test_init_reporting_session(tmpdir):48 with change_dir(tmpdir.strpath):49 _init_reporting_session(".")50 def test_init_reporting_session_with_valid_custom_report_saving_strategy(tmpdir):51 with change_dir(tmpdir.strpath):52 with env_vars(LCC_SAVE_REPORT="at_each_test"):53 _init_reporting_session(".")54 def test_init_reporting_session_with_invalid_custom_report_saving_strategy(tmpdir):55 with change_dir(tmpdir.strpath):56 with env_vars(LCC_SAVE_REPORT="foobar"):57 with pytest.raises(ValueError, match="Invalid expression"):58 _init_reporting_session(".")59 def test_scenario_passed(tmpdir):60 feature = u"""Feature: do some computations61 62Scenario: do a simple addition63 Given a is 264 And b is 265 Then a + b is equal to 466"""67 report = run_behave_tests(tmpdir, feature, STEPS)68 test = get_last_test(report)69 assert test.parent_suite.name == "do_some_computations"70 assert test.parent_suite.description == "Feature: do some computations"71 assert test.status == "passed"72 assert test.name == "do_a_simple_addition"73 assert test.description == "Scenario: do a simple addition"74 steps = test.get_steps()75 assert steps[0].description == "Given a is 2"76 assert steps[0].get_logs()[0].message == "a = 2"77 assert steps[1].description == "And b is 2"78 assert steps[1].get_logs()[0].message == "b = 2"79 assert steps[2].description == "Then a + b is equal to 4"80 assert steps[2].get_logs()[0].description == "Expect 2 + 2 to be equal to 4"81 assert steps[2].get_logs()[0].is_successful is True82 def test_scenario_failed(tmpdir):83 feature = u"""Feature: do some computations84 85Scenario: do a simple addition86 Given a is 287 And b is 288 Then a + b is equal to 589"""90 report = run_behave_tests(tmpdir, feature, STEPS)91 test = get_last_test(report)92 assert test.parent_suite.name == "do_some_computations"93 assert test.parent_suite.description == "Feature: do some computations"94 assert test.status == "failed"95 assert test.name == "do_a_simple_addition"96 assert test.description == "Scenario: do a simple addition"97 steps = test.get_steps()98 assert steps[0].description == "Given a is 2"99 assert steps[0].get_logs()[0].message == "a = 2"100 assert steps[1].description == "And b is 2"101 assert steps[1].get_logs()[0].message == "b = 2"102 assert steps[2].description == "Then a + b is equal to 5"103 assert steps[2].get_logs()[0].description == "Expect 2 + 2 to be equal to 5"104 assert steps[2].get_logs()[0].is_successful is False105 def test_scenario_unicode(tmpdir):106 feature = u"""Feature: do some computations éà 107 108Scenario: do a simple addition éà 109 Given a is 2 éà 110 And b is 2111 Then a + b is equal to 4112"""113 report = run_behave_tests(tmpdir, feature, STEPS)114 test = get_last_test(report)115 assert test.parent_suite.name == "do_some_computations_ea"116 assert test.parent_suite.description == u"Feature: do some computations éà "117 assert test.status == "passed"118 assert test.name == "do_a_simple_addition_ea"119 assert test.description == u"Scenario: do a simple addition éà "120 steps = test.get_steps()121 assert steps[0].description == u"Given a is 2 éà "122 assert steps[0].get_logs()[0].message == "a = 2"123 assert steps[1].description == "And b is 2"124 assert steps[1].get_logs()[0].message == "b = 2"125 assert steps[2].description == "Then a + b is equal to 4"126 assert steps[2].get_logs()[0].description == "Expect 2 + 2 to be equal to 4"127 assert steps[2].get_logs()[0].is_successful is True128 def test_tags(tmpdir):129 feature = u"""@tag1130Feature: do some computations131@tag2132Scenario: do a simple addition133 Given a is 2134 And b is 2135 Then a + b is equal to 4136"""137 report = run_behave_tests(tmpdir, feature, STEPS)138 test = get_last_test(report)139 assert test.parent_suite.tags == ["tag1"]140 assert test.tags == ["tag2"]141 def test_scenario_outline(tmpdir):142 feature = u"""Feature: do some computations143 144 Scenario Outline: addition145 Given a is <a>146 Given b is <b>147 Then a + b is equal to <c>148 Examples:149 | a | b | c |150 | 2 | 2 | 4 |151 | 1 | 1 | 3 |152 | 1 | 5 | 6 |153"""154 report = run_behave_tests(tmpdir, feature, STEPS)155 suite = get_last_suite(report)156 test_1, test_2, test_3 = suite.get_tests()157 assert test_1.name == "addition_1_1"158 assert test_1.description == "Scenario: addition -- @1.1"159 assert test_1.status == "passed"160 assert test_2.name == "addition_1_2"161 assert test_2.description == "Scenario: addition -- @1.2"162 assert test_2.status == "failed"163 assert test_3.name == "addition_1_3"164 assert test_3.description == "Scenario: addition -- @1.3"165 assert test_3.status == "passed"166 def test_custom_report_dir(tmpdir):167 feature = u"""Feature: do some computations168 Scenario: do a simple addition169 Given a is 2170 And b is 2171 Then a + b is equal to 4172 """173 report_dir = os.path.join(tmpdir.strpath, "custom_report_dir")174 with env_vars(LCC_REPORT_DIR=report_dir):175 report = run_behave_tests(tmpdir, feature, STEPS, expected_report_dir=report_dir)176 assert report is not None177 def test_custom_reporting(tmpdir):178 feature = u"""Feature: do some computations179 Scenario: do a simple addition180 Given a is 2181 And b is 2182 Then a + b is equal to 4183 """184 with env_vars(LCC_REPORTING="-html"):185 run_behave_tests(tmpdir, feature, STEPS)186 assert tmpdir.join("report", "report.js").exists()187 assert not tmpdir.join("report", "report.html").exists()188 def test_play_well_with_existing_hook(tmpdir):189 feature = u"""Feature: do some computations190 Scenario: do a simple addition191 Given a is 2192 And b is 2193 Then a + b is equal to 4194 """195 env = u"""import os196import os.path197from lemoncheesecake.bdd.behave import install_hooks198def after_all(_):199 os.mkdir(os.path.join(r"{tmpdir}", "iwashere"))200install_hooks()201""".format(tmpdir=tmpdir.strpath)202 with env_vars(LCC_REPORTING="-html"):203 run_behave_tests(tmpdir, feature, STEPS, env_content=env)204 assert tmpdir.join("report", "report.js").exists()205 assert not tmpdir.join("report", "report.html").exists()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!