vendredi 27 mars 2020

JUnit 5 PyTest-like library supporting sharable test fixtures and assertions

As the title says: I am looking for JUnit 5 library with similar functionalities to those found in PyTest testing framework. I am expecially interested in re-using test fixtures between test cases including the environment state.

Mocking InetSocketAddress.getAddress().getHostAddress() fails

Here's what I tried:

@Test
public void local_host_test() {

    InetAddress inetAddress = Mockito.mock(InetAddress.class);
    Mockito.when(inetAddress.getHostAddress()).thenReturn("127.0.0.1");
    InetSocketAddress inetSocketAddress = Mockito.mock(InetSocketAddress.class);
    Mockito.when(inetSocketAddress.getAddress()).thenReturn(inetAddress);
    Mockito.when(inetSocketAddress.getPort()).thenReturn(22);

    Assert.assertThat(inetSocketAddress.getAddress().getHostAddress(), Is.is("127.0.0.1"));
}

I also mocked the intermediate step, i.e., InetAddress, but it complains as shown below:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.creation.cglib.ClassImposterizer (file:/Users/iluvtrollhd/.m2/repository/org/mockito/mockito-all/1.10.19/mockito-all-1.10.19.jar) to constructor java.net.InetAddress()
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.creation.cglib.ClassImposterizer
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

java.lang.NullPointerException
    at java.base/java.net.InetSocketAddress.getAddress(InetSocketAddress.java:325)
    at InetSocketAddressTest.local_host_test(InetSocketAddressTest.java:17)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)


Process finished with exit code 255

What did I wrong?

Where can I find the implementation of Selenium Interfaces?

Maybe a dumb question, but where can I find the implementation of Selenium Interfaces. For example, WebElement is an interface that has a click() method amongst others. Where can I find how Selenium has implemented this click()? There should be an implementation of this interface somewhere. I can't seem to find it.

Another example, Alert. We conveniently switch to an Alert and getText() or dismiss(). But where can I find exactly what selenium is doing under the hood with these functions.

Thanks much!

Rust: what is the proper way to define custom attributes for conditional compilation?

I'd like to be able to pass a flag to cargo test to enable logging in my tests, when I need to debug them.

I've come up with something like:

#[cfg(logging)]
use crate::logging;

#[test]
fn mytest() {
    #[cfg(logging)]
    logging::enable();
    // ..
    assert!(true);
}

Then I can enable the logs with

RUSTFLAGS="--cfg logging" cargo test

It works but it feels like I'm abusing the rustc flag system. It also has the side effect of recompiling all the crates with my logging flag, which (besides the fact that it takes ages) may be an issue if this flag is used by one of my dependency some day.

Is there a better way to define and use custom attributes? I could add a feature to my cargo manifest, but this is not really a feature since it's just for the tests.


Just a note: normally, cargo test captures the output of each test. Unfortunately that doesn't seem to work when using tracing and tracing_subscriber for logging.

How to test a view that creates a new Organization(entity) in database in Django?

I'm new to Unit Testing. I have a view which takes an AJAX request and creates a new Organization to my database. How do I test it?

Here is my view:

@csrf_exempt
def newOrg(request):
    if request.method == 'POST':
        param = json.loads(request.body)
        org = param.get('org')
        Organization.objects.create(orgname=org)
        return JsonResponse({'status':200})

The url used:

('ajax/newOrg/', views_admin.newOrg, name='ajax_newOrg'),

jeudi 26 mars 2020

How to Get Code Coverage using Template Unit Test

I am looking for a method for finding the code coverage when using TUT (Template Unit Test) framework.

Registering new feature test

I'm creating a new test

php artisan make:test

I wrote new test in new file but it doesn't work (no matter what I write, any time I has success testing). Propably I must register this test. Where I can do that?