def test_multiple_steps_on_different_threads():
    def thread_func(i):
        lcc.set_step(str(i), detached=True)
        time.sleep(0.001)
        lcc.log_info(str(i))
        lcc.end_step(str(i))

    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Some test")
        def sometest(self):
            threads = [
                lcc.Thread(target=thread_func, args=(i, )) for i in range(3)
            ]
            for thread in threads:
                thread.start()
            for thread in threads:
                thread.join()

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    remainings = list(range(3))

    for step in test.steps:
        remainings.remove(int(step.description))
        assert len(step.entries) == 1
        assert step.entries[0].message == step.description

    assert len(remainings) == 0
def test_log_url():
    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Some test")
        def sometest(self):
            lcc.log_url("http://example.com", "example")

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    assert test.steps[0].entries[0].description == "example"
    assert test.steps[0].entries[0].url == "http://example.com"
def test_default_step():
    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Some test")
        def sometest(self):
            lcc.log_info("do something")

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    assert test.status == "passed"
    assert test.steps[0].description == "Some test"
    assert test.steps[0].entries[0].level == "info"
    assert test.steps[0].entries[0].message == "do something"
def test_end_step_on_detached_step():
    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Some test")
        def sometest(self):
            lcc.set_step("step", detached=True)
            lcc.log_info("log")
            lcc.end_step("step")

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    assert test.status == "passed"
    assert test.steps[0].description == "step"
    assert test.steps[0].entries[0].level == "info"
    assert test.steps[0].entries[0].message == "log"
def test_require_failure():
    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Test 1")
        def test_1(self):
            lcc.require_that("somevalue", "foo", lcc.equal_to("bar"))

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    assert test.status == "failed"
    step = test.steps[0]
    assert "somevalue" in step.entries[0].description
    assert "bar" in step.entries[0].description
    assert step.entries[0].outcome is False
    assert "foo" in step.entries[0].details
def test_check_success():
    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Test 1")
        def test_1(self):
            lcc.check_that("somevalue", "foo", lcc.equal_to("foo"))

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    assert test.status == "passed"
    step = test.steps[0]
    assert "somevalue" in step.entries[0].description
    assert "foo" in step.entries[0].description
    assert step.entries[0].outcome is True
    assert "foo" in step.entries[0].details
def test_step_after_test_setup():
    @lcc.suite("mysuite")
    class mysuite:
        def setup_test(self, test):
            lcc.log_info("in test setup")

        @lcc.test("Some test")
        def sometest(self):
            lcc.log_info("do something")

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    assert test.status == "passed"
    assert test.steps[0].description == "Setup test"
    assert test.steps[0].entries[0].level == "info"
    assert test.steps[0].entries[0].message == "in test setup"
    assert test.steps[1].description == "Some test"
    assert test.steps[1].entries[0].level == "info"
    assert test.steps[1].entries[0].message == "do something"
def test_concurrent_steps():
    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Some test")
        def sometest(self):
            lcc.set_step("step 1", detached=True)
            lcc.set_step("step 2", detached=True)
            lcc.log_info("do something else", step="step 2")
            lcc.log_info("do something", step="step 1")

    report = run_suite_class(mysuite)

    test = get_last_test(report)
    assert test.status == "passed"
    assert test.steps[0].description == "step 1"
    assert test.steps[0].entries[0].level == "info"
    assert test.steps[0].entries[0].message == "do something"
    assert test.steps[1].description == "step 2"
    assert test.steps[1].entries[0].level == "info"
    assert test.steps[1].entries[0].message == "do something else"
def test_unicode(tmpdir):
    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("some test")
        def sometest(self):
            lcc.set_step(u"éééààà")
            lcc.check_that(u"éééààà", 1, lcc.equal_to(1))
            lcc.log_info(u"éééààà")
            lcc.save_attachment_content("A" * 1024, u"somefileààà", u"éééààà")

    report = run_suite_class(mysuite, tmpdir=tmpdir)

    test = get_last_test(report)
    assert test.status == "passed"
    step = test.steps[0]
    assert step.description == u"éééààà"
    assert u"éééààà" in step.entries[0].description
    assert "1" in step.entries[0].description
    assert step.entries[1].message == u"éééààà"
    assert_attachment(step.entries[2], u"somefileààà", u"éééààà", False,
                      "A" * 1024, make_file_reader(encoding="utf8"))
def test_exception_in_thread_detached_step():
    def thread_func():
        with lcc.detached_step("step"):
            lcc.log_info("doing something")
            raise Exception("this_is_an_exception")

    @lcc.suite("MySuite")
    class mysuite:
        @lcc.test("Some test")
        def sometest(self):
            thread = lcc.Thread(target=thread_func)
            thread.start()
            thread.join()

    report = run_suite_class(mysuite)

    test = get_last_test(report)

    assert test.status == "failed"
    assert test.steps[0].description == "step"
    assert test.steps[0].entries[-1].level == "error"
    assert "this_is_an_exception" in test.steps[0].entries[-1].message