engineerjoe440 / ElectricPy

Electrical Engineering Formulas in Python
https://electricpy.readthedocs.io/en/latest/
MIT License
86 stars 16 forks source link

Format for test functions #37

Closed Lakshmikanth2001 closed 2 years ago

Lakshmikanth2001 commented 2 years ago

Can we organized all our test functions in this format

def test_distance():

    def test_0():
        p1 = Point(1, 2)
        p2 = Point(3, 4)
        assert geometry.distance(p1, p2) == 2*(2**0.5)

        p1 = Point(4, -6)
        p2 = Point(-2, -5)
        assert geometry.distance(p2, p1) ==  (37**0.5)

        p1 = Point(1.3, 2.3)
        p2 = Point(1.4, 2.4)

        d_output = geometry.distance(p1, p2)
        d_actual = 0.1*(2**0.5)

        assert_array_almost_equal(d_output, d_actual, decimal=6)

    def test_1():
        p1 = Point(1, 2)
        p2 = Point(1, 3)
        assert geometry.distance(p1, p2) == 1

        p1 = Point(2.0, 1)
        p2 = Point(3.0, 1)
        assert geometry.distance(p1, p2) == 1

    for i in range(2):
        exec("test_{}()".format(i))

Where all test function contain at least two test case with increasing level of complexity

Lakshmikanth2001 commented 2 years ago

Sir, can you provide your views about this

engineerjoe440 commented 2 years ago

Hi @Lakshmikanth2001!

Hmm.... I'm struggling to see the purpose of nesting test functions in this paradigm. Would you be able to educate me on the benefit and additional reasoning? I do think that it's valuable to exercise all "corner-cases" of the functionality of the library, but I don't know that I agree with "ramping up" functional tests. I'd rather test the "happy-path" and the corner cases.

That said, I'm interested to hear what the benefit of this pattern is! Perhaps I'm overlooking something valuable!

Lakshmikanth2001 commented 2 years ago

The Advantages are people can contribute to test cases. First, they will review test_0 to test_{i} so the complexity of each test case improves they can properly verify where doest their test case fit, or whether the test is already covered by existing methods

Lakshmikanth2001 commented 2 years ago

Sir Any update regarding this issue

engineerjoe440 commented 2 years ago

The Advantages are people can contribute to test cases. First, they will review test_0 to test_{i} so the complexity of each test case improves they can properly verify where doest their test case fit, or whether the test is already covered by existing methods

While that's true, the "pythonic" way to write tests for pytest is to write descriptive names to tell what the purpose of the test is. More test-functions can be used for the same core function, and simply be altered to more accurately describe the purpose of the test.

Lakshmikanth2001 commented 2 years ago

We need to make TestSuiets to do that, I have to learn how to implement them